Skip to content

TST: GroupBy(..., as_index=True).agg() drops index #33098

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 10 commits into from
Jun 15, 2020
9 changes: 9 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,15 @@ def test_aggregate_mixed_types():
tm.assert_frame_equal(result, expected)


def test_aggregate_categorical_lost_index():
# GH: 28641 groupby drops index, when grouping over categorical column with min/max
ds = pd.Series(["b"], dtype="category").cat.as_ordered()
df = pd.DataFrame({"A": [1997], "B": ds})
result = df.groupby("A").agg({"B": "min"})
expected = pd.DataFrame({"B": ["b"]}, index=pd.Index([1997], name="A"))
tm.assert_frame_equal(result, expected)


@pytest.mark.xfail(reason="Not implemented.")
def test_aggregate_udf_na_extension_type():
# https://github.com/pandas-dev/pandas/pull/31359
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,3 +1380,13 @@ def test_groupby_agg_non_numeric():

result = df.groupby([1, 2, 1]).nunique()
tm.assert_frame_equal(result, expected)


def test_groupy_first_returns_categorical():
# GH 28641: groupby drops index, when grouping over categorical column with first
df = pd.DataFrame(
{"A": [1997], "B": pd.Series(["b"], dtype="category").cat.as_ordered()}
)
result = df.groupby("A")["B"].first()
expected = pd.Series(["b"], index=pd.Index([1997], name="A"), name="B")
tm.assert_series_equal(result, expected)