Skip to content

TST: Determine groupby-transform not convert back type #50754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
20 changes: 20 additions & 0 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,26 @@ def test_groupby_transform_with_datetimes(func, values):
tm.assert_series_equal(result, expected)


def test_groupby_transform_dtype():
# GH 22243
df = DataFrame({"a": [1], "val": [1.35]})

result = df["val"].transform(lambda x: x.map(lambda y: f"+{y}"))
expected1 = Series(["+1.35"], name="val", dtype="object")
tm.assert_series_equal(result, expected1)

result = df.groupby("a")["val"].transform(lambda x: x.map(lambda y: f"+{y}"))
tm.assert_series_equal(result, expected1)

result = df.groupby("a")["val"].transform(lambda x: x.map(lambda y: f"+({y})"))
expected2 = Series(["+(1.35)"], name="val", dtype="object")
tm.assert_series_equal(result, expected2)

df["val"] = df["val"].astype(object)
result = df.groupby("a")["val"].transform(lambda x: x.map(lambda y: f"+{y}"))
tm.assert_series_equal(result, expected1)


@pytest.mark.parametrize("func", ["cumsum", "cumprod", "cummin", "cummax"])
def test_transform_absent_categories(func):
# GH 16771
Expand Down