@@ -255,19 +255,19 @@ def test_apply_with_mixed_dtype():
255
255
"foo2" : ["one" , "two" , "two" , "three" , "one" , "two" ],
256
256
}
257
257
)
258
- result = df .apply (lambda x : x , axis = 1 ). dtypes
259
- expected = df . dtypes
260
- tm .assert_series_equal (result , expected )
258
+ result = df .apply (lambda x : x , axis = 1 )
259
+ expected = df
260
+ tm .assert_frame_equal (result , expected )
261
261
262
262
# GH 3610 incorrect dtype conversion with as_index=False
263
263
df = DataFrame ({"c1" : [1 , 2 , 6 , 6 , 8 ]})
264
264
df ["c2" ] = df .c1 / 2.0
265
- result1 = df .groupby ("c2" ).mean ().reset_index (). c2
266
- result2 = df .groupby ("c2" , as_index = False ).mean (). c2
267
- tm .assert_series_equal (result1 , result2 )
265
+ result1 = df .groupby ("c2" ).mean ().reset_index ()
266
+ result2 = df .groupby ("c2" , as_index = False ).mean ()
267
+ tm .assert_frame_equal (result1 , result2 )
268
268
269
269
270
- def test_groupby_as_index_apply ():
270
+ def test_groupby_as_index_apply (as_index ):
271
271
# GH #4648 and #3417
272
272
df = DataFrame (
273
273
{
@@ -276,27 +276,35 @@ def test_groupby_as_index_apply():
276
276
"time" : range (6 ),
277
277
}
278
278
)
279
+ gb = df .groupby ("user_id" , as_index = as_index )
279
280
280
- g_as = df .groupby ("user_id" , as_index = True )
281
- g_not_as = df .groupby ("user_id" , as_index = False )
282
-
283
- res_as = g_as .head (2 ).index
284
- res_not_as = g_not_as .head (2 ).index
285
- exp = Index ([0 , 1 , 2 , 4 ])
286
- tm .assert_index_equal (res_as , exp )
287
- tm .assert_index_equal (res_not_as , exp )
288
-
289
- res_as_apply = g_as .apply (lambda x : x .head (2 )).index
290
- res_not_as_apply = g_not_as .apply (lambda x : x .head (2 )).index
281
+ expected = DataFrame (
282
+ {
283
+ "item_id" : ["b" , "b" , "a" , "a" ],
284
+ "user_id" : [1 , 2 , 1 , 3 ],
285
+ "time" : [0 , 1 , 2 , 4 ],
286
+ },
287
+ index = [0 , 1 , 2 , 4 ],
288
+ )
289
+ result = gb .head (2 )
290
+ tm .assert_frame_equal (result , expected )
291
291
292
292
# apply doesn't maintain the original ordering
293
293
# changed in GH5610 as the as_index=False returns a MI here
294
- exp_not_as_apply = Index ([0 , 2 , 1 , 4 ])
295
- tp = [(1 , 0 ), (1 , 2 ), (2 , 1 ), (3 , 4 )]
296
- exp_as_apply = MultiIndex .from_tuples (tp , names = ["user_id" , None ])
297
-
298
- tm .assert_index_equal (res_as_apply , exp_as_apply )
299
- tm .assert_index_equal (res_not_as_apply , exp_not_as_apply )
294
+ if as_index :
295
+ tp = [(1 , 0 ), (1 , 2 ), (2 , 1 ), (3 , 4 )]
296
+ index = MultiIndex .from_tuples (tp , names = ["user_id" , None ])
297
+ else :
298
+ index = Index ([0 , 2 , 1 , 4 ])
299
+ expected = DataFrame (
300
+ {
301
+ "item_id" : list ("baba" ),
302
+ "time" : [0 , 2 , 1 , 4 ],
303
+ },
304
+ index = index ,
305
+ )
306
+ result = gb .apply (lambda x : x .head (2 ))
307
+ tm .assert_frame_equal (result , expected )
300
308
301
309
302
310
def test_groupby_as_index_apply_str ():
0 commit comments