Skip to content

Commit d81882b

Browse files
authored
TST/CLN: Improve some groupby.apply tests (#60620)
1 parent 37f4392 commit d81882b

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
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():

0 commit comments

Comments
 (0)