Skip to content

Commit a6943ae

Browse files
TST: groupby.first/last retains categorical dtype (pandas-dev#43153)
1 parent 1625f28 commit a6943ae

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pandas/tests/groupby/test_categorical.py

+12
Original file line numberDiff line numberDiff line change
@@ -1741,3 +1741,15 @@ def test_groupby_categorical_indices_unused_categories():
17411741
assert result.keys() == expected.keys()
17421742
for key in result.keys():
17431743
tm.assert_numpy_array_equal(result[key], expected[key])
1744+
1745+
1746+
@pytest.mark.parametrize("func", ["first", "last"])
1747+
def test_groupby_last_first_preserve_categoricaldtype(func):
1748+
# GH#33090
1749+
df = DataFrame({"a": [1, 2, 3]})
1750+
df["b"] = df["a"].astype("category")
1751+
result = getattr(df.groupby("a")["b"], func)()
1752+
expected = Series(
1753+
Categorical([1, 2, 3]), name="b", index=Index([1, 2, 3], name="a")
1754+
)
1755+
tm.assert_series_equal(expected, result)

0 commit comments

Comments
 (0)