Skip to content

Commit eec8a83

Browse files
committed
BUG: fix groupby.apply index name bug, close pandas-dev#1701
1 parent f5a74d4 commit eec8a83

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pandas 0.8.2
3434
**Bug fixes**
3535

3636
- Fix MM-YYYY time series indexing case (#1672)
37+
- Fix case where Categorical group key was not being passed into index in
38+
GroupBy result (#1701)
3739

3840
pandas 0.8.1
3941
============

pandas/core/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,8 @@ def _get_index():
12891289

12901290
if isinstance(values[0], dict):
12911291
# # GH #823
1292-
return DataFrame(values, index=keys).stack()
1292+
index = _get_index()
1293+
return DataFrame(values, index=index).stack()
12931294

12941295
if isinstance(values[0], (Series, dict)):
12951296
return self._concat_objects(keys, values,

pandas/tests/test_groupby.py

+11
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,17 @@ def test_apply_corner(self):
14261426
expected = self.tsframe * 2
14271427
assert_frame_equal(result, expected)
14281428

1429+
def test_apply_use_categorical_name(self):
1430+
from pandas import qcut
1431+
cats = qcut(self.df.C, 4)
1432+
1433+
def get_stats(group):
1434+
return {'min': group.min(), 'max': group.max(),
1435+
'count': group.count(), 'mean': group.mean()}
1436+
1437+
result = self.df.groupby(cats).D.apply(get_stats)
1438+
self.assertEquals(result.index.names[0], 'C')
1439+
14291440
def test_transform_mixed_type(self):
14301441
index = MultiIndex.from_arrays([[0, 0, 0, 1, 1, 1],
14311442
[1, 2, 3, 1, 2, 3]])

0 commit comments

Comments
 (0)