Skip to content

Commit 2eee4a8

Browse files
authored
Merge branch 'main' into fix59933
2 parents 163dfe3 + d81882b commit 2eee4a8

File tree

2 files changed

+42
-51
lines changed

2 files changed

+42
-51
lines changed

pandas/tests/groupby/test_apply.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,19 @@ def test_apply_with_mixed_dtype():
255255
"foo2": ["one", "two", "two", "three", "one", "two"],
256256
}
257257
)
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)
261261

262262
# GH 3610 incorrect dtype conversion with as_index=False
263263
df = DataFrame({"c1": [1, 2, 6, 6, 8]})
264264
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)
268268

269269

270-
def test_groupby_as_index_apply():
270+
def test_groupby_as_index_apply(as_index):
271271
# GH #4648 and #3417
272272
df = DataFrame(
273273
{
@@ -276,27 +276,35 @@ def test_groupby_as_index_apply():
276276
"time": range(6),
277277
}
278278
)
279+
gb = df.groupby("user_id", as_index=as_index)
279280

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

292292
# apply doesn't maintain the original ordering
293293
# 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)
300308

301309

302310
def test_groupby_as_index_apply_str():

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)