@@ -227,6 +227,22 @@ def fast(group):
227227 tm .assert_frame_equal (fast_df , slow_df )
228228
229229
230+ def test_apply_fast_slow_identical_index ():
231+ # GH#44803
232+ df = DataFrame (
233+ {
234+ "name" : ["Alice" , "Bob" , "Carl" ],
235+ "age" : [20 , 21 , 20 ],
236+ }
237+ ).set_index ("name" )
238+
239+ grp_by_same_value = df .groupby (["age" ], group_keys = False ).apply (lambda group : group )
240+ grp_by_copy = df .groupby (["age" ], group_keys = False ).apply (
241+ lambda group : group .copy ()
242+ )
243+ tm .assert_frame_equal (grp_by_same_value , grp_by_copy )
244+
245+
230246@pytest .mark .parametrize (
231247 "func" ,
232248 [
@@ -1463,3 +1479,37 @@ def f_4(grp):
14631479 e .loc ["Pony" ] = np .nan
14641480 e .name = None
14651481 tm .assert_series_equal (result , e )
1482+
1483+
1484+ def test_nonreducer_nonstransform ():
1485+ # GH3380, GH60619
1486+ # Was originally testing mutating in a UDF; now kept as an example
1487+ # of using apply with a nonreducer and nontransformer.
1488+ df = DataFrame (
1489+ {
1490+ "cat1" : ["a" ] * 8 + ["b" ] * 6 ,
1491+ "cat2" : ["c" ] * 2
1492+ + ["d" ] * 2
1493+ + ["e" ] * 2
1494+ + ["f" ] * 2
1495+ + ["c" ] * 2
1496+ + ["d" ] * 2
1497+ + ["e" ] * 2 ,
1498+ "val" : np .random .default_rng (2 ).integers (100 , size = 14 ),
1499+ }
1500+ )
1501+
1502+ def f (x ):
1503+ x = x .copy ()
1504+ x ["rank" ] = x .val .rank (method = "min" )
1505+ return x .groupby ("cat2" )["rank" ].min ()
1506+
1507+ expected = DataFrame (
1508+ {
1509+ "cat1" : list ("aaaabbb" ),
1510+ "cat2" : list ("cdefcde" ),
1511+ "rank" : [3.0 , 2.0 , 5.0 , 1.0 , 2.0 , 4.0 , 1.0 ],
1512+ }
1513+ ).set_index (["cat1" , "cat2" ])["rank" ]
1514+ result = df .groupby ("cat1" ).apply (f )
1515+ tm .assert_series_equal (result , expected )
0 commit comments