Skip to content

Commit a6d5db7

Browse files
authored
TST: groupby string dtype (#52599)
* TST: groupby string dtype * compare dataframe and formatting
1 parent d12c618 commit a6d5db7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pandas/tests/groupby/test_groupby.py

+20
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,26 @@ def test_single_element_list_grouping():
27392739
assert result == expected
27402740

27412741

2742+
def test_groupby_string_dtype():
2743+
# GH 40148
2744+
df = DataFrame({"str_col": ["a", "b", "c", "a"], "num_col": [1, 2, 3, 2]})
2745+
df["str_col"] = df["str_col"].astype("string")
2746+
expected = DataFrame(
2747+
{
2748+
"str_col": [
2749+
"a",
2750+
"b",
2751+
"c",
2752+
],
2753+
"num_col": [1.5, 2.0, 3.0],
2754+
}
2755+
)
2756+
expected["str_col"] = expected["str_col"].astype("string")
2757+
grouped = df.groupby("str_col", as_index=False)
2758+
result = grouped.mean()
2759+
tm.assert_frame_equal(result, expected)
2760+
2761+
27422762
@pytest.mark.parametrize(
27432763
"level_arg, multiindex", [([0], False), ((0,), False), ([0], True), ((0,), True)]
27442764
)

0 commit comments

Comments
 (0)