Skip to content

BUG: groupby nunique not respecting observed #45143

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 1 commit into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ Groupby/resample/rolling
- Bug in :meth:`GroupBy.nth` failing on ``axis=1`` (:issue:`43926`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not respecting right bound on centered datetime-like windows, if the index contain duplicates (:issue:`3944`)
- Bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` when using a :class:`pandas.api.indexers.BaseIndexer` subclass that returned unequal start and end arrays would segfault instead of raising a ``ValueError`` (:issue:`44470`)
- Bug in :meth:`Groupby.nunique` not respecting ``observed=True`` for Categorical grouping columns (:issue:`45128`)
- Bug in :meth:`GroupBy.head` and :meth:`GroupBy.tail` not dropping groups with ``NaN`` when ``dropna=True`` (:issue:`45089`)
- Fixed bug in :meth:`GroupBy.__iter__` after selecting a subset of columns in a :class:`GroupBy` object, which returned all columns instead of the chosen subset (:issue:`#44821`)
- Bug in :meth:`Groupby.rolling` when non-monotonic data passed, fails to correctly raise ``ValueError`` (:issue:`43909`)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,7 @@ def _iterate_column_groupbys(self, obj: DataFrame | Series):
selection=colname,
grouper=self.grouper,
exclusions=self.exclusions,
observed=self.observed,
)

def _apply_to_column_groupbys(self, func, obj: DataFrame | Series) -> DataFrame:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,3 +1779,18 @@ def test_groupby_last_first_preserve_categoricaldtype(func):
Categorical([1, 2, 3]), name="b", index=Index([1, 2, 3], name="a")
)
tm.assert_series_equal(expected, result)


def test_groupby_categorical_observed_nunique():
# GH#45128
df = DataFrame({"a": [1, 2], "b": [1, 2], "c": [10, 11]})
df = df.astype(dtype={"a": "category", "b": "category"})
result = df.groupby(["a", "b"], observed=True).nunique()["c"]
expected = Series(
[1, 1],
index=MultiIndex.from_arrays(
[CategoricalIndex([1, 2], name="a"), CategoricalIndex([1, 2], name="b")]
),
name="c",
)
tm.assert_series_equal(result, expected)