Skip to content

Commit c320a15

Browse files
committed
Merge pull request #4670 from hayd/groupby_as_index
Groupby as index
2 parents 4226afe + e8530df commit c320a15

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
231231
of a duplicate index (:issue:`4359`)
232232
- In ``to_json``, fix date handling so milliseconds are the default timestamp
233233
as the docstring says (:issue:`4362`).
234+
- ``as_index`` is no longer ignored when doing groupby apply (:issue:`4648`), (:issue:`3417`)
234235
- JSON NaT handling fixed, NaTs are now serialised to `null` (:issue:`4498`)
235236
- Fixed JSON handling of escapable characters in JSON object keys (:issue:`4593`)
236237
- 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: 30 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,34 @@ 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+
ind = Index(list('abcde'))
1206+
df = DataFrame([[1, 2], [2, 3], [1, 4], [1, 5], [2, 6]], index=ind)
1207+
res = df.groupby(0, as_index=False).apply(lambda x: x).index
1208+
assert_index_equal(res, ind)
1209+
11811210
def test_groupby_multiple_key(self):
11821211
df = tm.makeTimeDataFrame()
11831212
grouped = df.groupby([lambda x: x.year,

0 commit comments

Comments
 (0)