Skip to content

Commit 37f4392

Browse files
authored
TST/CLN: Remove groupby tests with mutation (#60619)
1 parent 9cf4911 commit 37f4392

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

pandas/tests/groupby/test_apply_mutate.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@ def test_mutate_groups():
3838
}
3939
)
4040

41-
def f_copy(x):
41+
def f(x):
4242
x = x.copy()
4343
x["rank"] = x.val.rank(method="min")
4444
return x.groupby("cat2")["rank"].min()
4545

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)
5355

5456

5557
def test_no_mutate_but_looks_like():
@@ -61,22 +63,3 @@ def test_no_mutate_but_looks_like():
6163
result1 = df.groupby("key", group_keys=True).apply(lambda x: x[:].value)
6264
result2 = df.groupby("key", group_keys=True).apply(lambda x: x.value)
6365
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

Comments
 (0)