@@ -38,18 +38,20 @@ def test_mutate_groups():
38
38
}
39
39
)
40
40
41
- def f_copy (x ):
41
+ def f (x ):
42
42
x = x .copy ()
43
43
x ["rank" ] = x .val .rank (method = "min" )
44
44
return x .groupby ("cat2" )["rank" ].min ()
45
45
46
- def f_no_copy (x ):
47
- x ["rank" ] = x .val .rank (method = "min" )
48
- return x .groupby ("cat2" )["rank" ].min ()
49
-
50
- grpby_copy = df .groupby ("cat1" ).apply (f_copy )
51
- grpby_no_copy = df .groupby ("cat1" ).apply (f_no_copy )
52
- tm .assert_series_equal (grpby_copy , grpby_no_copy )
46
+ expected = pd .DataFrame (
47
+ {
48
+ "cat1" : list ("aaaabbb" ),
49
+ "cat2" : list ("cdefcde" ),
50
+ "rank" : [3.0 , 2.0 , 5.0 , 1.0 , 2.0 , 4.0 , 1.0 ],
51
+ }
52
+ ).set_index (["cat1" , "cat2" ])["rank" ]
53
+ result = df .groupby ("cat1" ).apply (f )
54
+ tm .assert_series_equal (result , expected )
53
55
54
56
55
57
def test_no_mutate_but_looks_like ():
@@ -61,22 +63,3 @@ def test_no_mutate_but_looks_like():
61
63
result1 = df .groupby ("key" , group_keys = True ).apply (lambda x : x [:].value )
62
64
result2 = df .groupby ("key" , group_keys = True ).apply (lambda x : x .value )
63
65
tm .assert_series_equal (result1 , result2 )
64
-
65
-
66
- def test_apply_function_with_indexing ():
67
- # GH: 33058
68
- df = pd .DataFrame (
69
- {"col1" : ["A" , "A" , "A" , "B" , "B" , "B" ], "col2" : [1 , 2 , 3 , 4 , 5 , 6 ]}
70
- )
71
-
72
- def fn (x ):
73
- x .loc [x .index [- 1 ], "col2" ] = 0
74
- return x .col2
75
-
76
- result = df .groupby (["col1" ], as_index = False ).apply (fn )
77
- expected = pd .Series (
78
- [1 , 2 , 0 , 4 , 5 , 0 ],
79
- index = range (6 ),
80
- name = "col2" ,
81
- )
82
- tm .assert_series_equal (result , expected )
0 commit comments