|
12 | 12 | from pandas.core.groupby import GroupByError, SpecificationError, DataError
|
13 | 13 | from pandas.core.series import Series
|
14 | 14 | 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) |
16 | 17 | from pandas.compat import(
|
17 | 18 | range, long, lrange, StringIO, lmap, lzip, map, zip, builtins, OrderedDict
|
18 | 19 | )
|
@@ -1178,6 +1179,34 @@ def test_groupby_as_index_corner(self):
|
1178 | 1179 | self.assertRaises(ValueError, self.df.groupby,
|
1179 | 1180 | lambda x: x.lower(), as_index=False, axis=1)
|
1180 | 1181 |
|
| 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 | + |
1181 | 1210 | def test_groupby_multiple_key(self):
|
1182 | 1211 | df = tm.makeTimeDataFrame()
|
1183 | 1212 | grouped = df.groupby([lambda x: x.year,
|
|
0 commit comments