Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ def fast(group):
tm.assert_frame_equal(fast_df, slow_df)


def test_apply_fast_slow_identical_index():
# GH#44803
df = DataFrame(
{
"name": ["Alice", "Bob", "Carl"],
"age": [20, 21, 20],
}
).set_index("name")

grp_by_same_value = df.groupby(["age"], group_keys=False).apply(lambda group: group)
grp_by_copy = df.groupby(["age"], group_keys=False).apply(
lambda group: group.copy()
)
tm.assert_frame_equal(grp_by_same_value, grp_by_copy)


@pytest.mark.parametrize(
"func",
[
Expand Down Expand Up @@ -1463,3 +1479,37 @@ def f_4(grp):
e.loc["Pony"] = np.nan
e.name = None
tm.assert_series_equal(result, e)


def test_nonreducer_nonstransform():
# GH3380, GH60619
# Was originally testing mutating in a UDF; now kept as an example
# of using apply with a nonreducer and nontransformer.
df = DataFrame(
{
"cat1": ["a"] * 8 + ["b"] * 6,
"cat2": ["c"] * 2
+ ["d"] * 2
+ ["e"] * 2
+ ["f"] * 2
+ ["c"] * 2
+ ["d"] * 2
+ ["e"] * 2,
"val": np.random.default_rng(2).integers(100, size=14),
}
)

def f(x):
x = x.copy()
x["rank"] = x.val.rank(method="min")
return x.groupby("cat2")["rank"].min()

expected = DataFrame(
{
"cat1": list("aaaabbb"),
"cat2": list("cdefcde"),
"rank": [3.0, 2.0, 5.0, 1.0, 2.0, 4.0, 1.0],
}
).set_index(["cat1", "cat2"])["rank"]
result = df.groupby("cat1").apply(f)
tm.assert_series_equal(result, expected)
65 changes: 0 additions & 65 deletions pandas/tests/groupby/test_apply_mutate.py

This file was deleted.

Loading