-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Labels
Description
Hello,
The following example definitely seems like a bug. The grouper is not dropped from the index of the resulting DataFrame, even when as_index = False.
Actually, even the aggregation step completely fails, so there may be more to it, as shown in this example.
import pandas as pd
d = {'foo': [10, 8, 4, 8, 4, 1, 1], 'bar': [10, 20, 30, 40, 50, 60, 70],
'baz': ['d', 'c', 'e', 'a', 'a', 'd', 'c']}
df = pd.DataFrame(d)
cat = pd.cut(df['foo'], np.linspace(0, 10, 3))
df['range'] = cat
groups = df.groupby(['range', 'baz'], as_index=False)
result = groups.agg('mean')result| range | baz | bar | foo | ||
|---|---|---|---|---|---|
| range | baz | ||||
| (0, 5] | a | NaN | NaN | NaN | NaN |
| c | NaN | NaN | NaN | NaN | |
| d | NaN | NaN | NaN | NaN | |
| e | NaN | NaN | NaN | NaN | |
| (5, 10] | a | NaN | NaN | NaN | NaN |
| c | NaN | NaN | NaN | NaN | |
| d | NaN | NaN | NaN | NaN | |
| e | NaN | NaN | NaN | NaN |
Compare to the expected result:
groups2 = df.groupby(['range', 'baz'], as_index=True)
expected = groups2.agg('mean').reset_index()expected| range | baz | bar | foo | |
|---|---|---|---|---|
| 0 | (0, 5] | a | 50 | 4 |
| 1 | (0, 5] | c | 70 | 1 |
| 2 | (0, 5] | d | 60 | 1 |
| 3 | (0, 5] | e | 30 | 4 |
| 4 | (5, 10] | a | 40 | 8 |
| 5 | (5, 10] | c | 20 | 8 |
| 6 | (5, 10] | d | 10 | 10 |
| 7 | (5, 10] | e | NaN |
pd.__version__
Out[181]: '0.15.1'