Skip to content

Commit

Permalink
TST: groupby.first/last retains categorical dtype (#43153)
Browse files Browse the repository at this point in the history
  • Loading branch information
debnathshoham committed Aug 31, 2021
1 parent 1625f28 commit a6943ae
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,3 +1741,15 @@ def test_groupby_categorical_indices_unused_categories():
assert result.keys() == expected.keys()
for key in result.keys():
tm.assert_numpy_array_equal(result[key], expected[key])


@pytest.mark.parametrize("func", ["first", "last"])
def test_groupby_last_first_preserve_categoricaldtype(func):
# GH#33090
df = DataFrame({"a": [1, 2, 3]})
df["b"] = df["a"].astype("category")
result = getattr(df.groupby("a")["b"], func)()
expected = Series(
Categorical([1, 2, 3]), name="b", index=Index([1, 2, 3], name="a")
)
tm.assert_series_equal(expected, result)

0 comments on commit a6943ae

Please sign in to comment.