Skip to content

Commit 824e9dd

Browse files
committed
FIX groupby apply ignores as_index
1 parent cba88ed commit 824e9dd

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
217217
of a duplicate index (:issue:`4359`)
218218
- In ``to_json``, fix date handling so milliseconds are the default timestamp
219219
as the docstring says (:issue:`4362`).
220+
- ``as_index`` is no longer ignored when doing groupby apply (:issue:`4648`), (:issue:`3417`)
220221
- JSON NaT handling fixed, NaTs are now serialised to `null` (:issue:`4498`)
221222
- Fixed JSON handling of escapable characters in JSON object keys (:issue:`4593`)
222223
- Fixed passing ``keep_default_na=False`` when ``na_values=None`` (:issue:`4318`)

pandas/core/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def _concat_objects(self, keys, values, not_indexed_same=False):
516516
result = result.reindex(ax)
517517
else:
518518
result = result.reindex_axis(ax, axis=self.axis)
519-
elif self.group_keys:
519+
elif self.group_keys and self.as_index:
520520
group_keys = keys
521521
group_levels = self.grouper.levels
522522
group_names = self.grouper.names

pandas/tests/test_groupby.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from pandas.core.groupby import GroupByError, SpecificationError, DataError
1313
from pandas.core.series import Series
1414
from pandas.util.testing import (assert_panel_equal, assert_frame_equal,
15-
assert_series_equal, assert_almost_equal)
15+
assert_series_equal, assert_almost_equal,
16+
assert_index_equal)
1617
from pandas.compat import(
1718
range, long, lrange, StringIO, lmap, lzip, map, zip, builtins, OrderedDict
1819
)
@@ -1178,6 +1179,30 @@ def test_groupby_as_index_corner(self):
11781179
self.assertRaises(ValueError, self.df.groupby,
11791180
lambda x: x.lower(), as_index=False, axis=1)
11801181

1182+
def test_groupby_as_index_apply(self):
1183+
# GH #4648 and #3417
1184+
df = DataFrame({'item_id': ['b', 'b', 'a', 'c', 'a', 'b'],
1185+
'user_id': [1,2,1,1,3,1],
1186+
'time': range(6)})
1187+
1188+
g_as = df.groupby('user_id', as_index=True)
1189+
g_not_as = df.groupby('user_id', as_index=False)
1190+
1191+
res_as = g_as.head(2).index
1192+
exp_as = MultiIndex.from_tuples([(1, 0), (1, 2), (2, 1), (3, 4)])
1193+
assert_index_equal(res_as, exp_as)
1194+
1195+
res_not_as = g_not_as.head(2).index
1196+
exp_not_as = Index([0, 2, 1, 4])
1197+
assert_index_equal(res_not_as, exp_not_as)
1198+
1199+
res_as = g_as.apply(lambda x: x.head(2)).index
1200+
assert_index_equal(res_not_as, exp_not_as)
1201+
1202+
res_not_as = g_not_as.apply(lambda x: x.head(2)).index
1203+
assert_index_equal(res_not_as, exp_not_as)
1204+
1205+
11811206
def test_groupby_multiple_key(self):
11821207
df = tm.makeTimeDataFrame()
11831208
grouped = df.groupby([lambda x: x.year,

0 commit comments

Comments
 (0)