Skip to content

Add tests for transform sum with series #58012

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
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
44 changes: 44 additions & 0 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,3 +1490,47 @@ def test_idxmin_idxmax_transform_args(how, skipna, numeric_only):
msg = f"DataFrameGroupBy.{how} with skipna=False encountered an NA value"
with pytest.raises(ValueError, match=msg):
gb.transform(how, skipna, numeric_only)


def test_transform_sum_one_column_no_matching_labels():
df = DataFrame({"X": [1.0]})
series = Series(["Y"])
result = df.groupby(series, as_index=False).transform("sum")
expected = DataFrame({"X": [1.0]})
tm.assert_frame_equal(result, expected)


def test_transform_sum_no_matching_labels():
df = DataFrame({"X": [1.0, -93204, 4935]})
series = Series(["A", "B", "C"])

result = df.groupby(series, as_index=False).transform("sum")
expected = DataFrame({"X": [1.0, -93204, 4935]})
tm.assert_frame_equal(result, expected)


def test_transform_sum_one_column_with_matching_labels():
df = DataFrame({"X": [1.0, -93204, 4935]})
series = Series(["A", "B", "A"])

result = df.groupby(series, as_index=False).transform("sum")
expected = DataFrame({"X": [4936.0, -93204, 4936.0]})
tm.assert_frame_equal(result, expected)


def test_transform_sum_one_column_with_missing_labels():
df = DataFrame({"X": [1.0, -93204, 4935]})
series = Series(["A", "C"])

result = df.groupby(series, as_index=False).transform("sum")
expected = DataFrame({"X": [1.0, -93204, np.nan]})
tm.assert_frame_equal(result, expected)


def test_transform_sum_one_column_with_matching_labels_and_missing_labels():
df = DataFrame({"X": [1.0, -93204, 4935]})
series = Series(["A", "A"])

result = df.groupby(series, as_index=False).transform("sum")
expected = DataFrame({"X": [-93203.0, -93203.0, np.nan]})
tm.assert_frame_equal(result, expected)