Skip to content

TST: groupby string dtype #52599

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 2 commits into from
Apr 12, 2023
Merged
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2709,6 +2709,15 @@ def test_single_element_list_grouping():
expected = [(1,), (2,)]
assert result == expected

def test_groupby_string_dtype():
#GH 40148
df = DataFrame({"str_col": ["a", "b", "c", "a"], "num_col": [1, 2, 3, 2]})
df["str_col"] = df['str_col'].astype("string")
expected = df.dtypes[0]
grouped = df.groupby("str_col", as_index=False)
avg = grouped.mean()
result = avg.dtypes[0]
tm.assert_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you instead compare the resulting DataFrame e.g.

result = df.groupby("str_col", as_index=False)
expected = DataFrame(...)
tm.assert_frame_equal(result, expected)

Copy link
Contributor Author

@srkds srkds Apr 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

My bad I didn't know about assert_frame_equal. Thanks!
I read about it here, It will compare dtypes. Sounds good 👍

Are these the required changes?

expected = DataFrame({"str_col": ["a", "b", "c",], "num_col": [1.5, 2.0, 3.0]})
expected["str_col"] = expected['str_col'].astype("string")
result=grouped.mean()
tm.assert_frame_equal(result,expected)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Thanks for confirming.


@pytest.mark.parametrize(
"level_arg, multiindex", [([0], False), ((0,), False), ([0], True), ((0,), True)]
Expand Down