Skip to content

Commit 1fcccda

Browse files
authored
BUG: groupby nunique not respecting observed (#45143)
1 parent cea27b4 commit 1fcccda

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ Groupby/resample/rolling
943943
- Bug in :meth:`GroupBy.nth` failing on ``axis=1`` (:issue:`43926`)
944944
- 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`)
945945
- 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`)
946+
- Bug in :meth:`Groupby.nunique` not respecting ``observed=True`` for Categorical grouping columns (:issue:`45128`)
946947
- Bug in :meth:`GroupBy.head` and :meth:`GroupBy.tail` not dropping groups with ``NaN`` when ``dropna=True`` (:issue:`45089`)
947948
- 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`)
948949
- Bug in :meth:`Groupby.rolling` when non-monotonic data passed, fails to correctly raise ``ValueError`` (:issue:`43909`)

pandas/core/groupby/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ def _iterate_column_groupbys(self, obj: DataFrame | Series):
14491449
selection=colname,
14501450
grouper=self.grouper,
14511451
exclusions=self.exclusions,
1452+
observed=self.observed,
14521453
)
14531454

14541455
def _apply_to_column_groupbys(self, func, obj: DataFrame | Series) -> DataFrame:

pandas/tests/groupby/test_categorical.py

+15
Original file line numberDiff line numberDiff line change
@@ -1779,3 +1779,18 @@ def test_groupby_last_first_preserve_categoricaldtype(func):
17791779
Categorical([1, 2, 3]), name="b", index=Index([1, 2, 3], name="a")
17801780
)
17811781
tm.assert_series_equal(expected, result)
1782+
1783+
1784+
def test_groupby_categorical_observed_nunique():
1785+
# GH#45128
1786+
df = DataFrame({"a": [1, 2], "b": [1, 2], "c": [10, 11]})
1787+
df = df.astype(dtype={"a": "category", "b": "category"})
1788+
result = df.groupby(["a", "b"], observed=True).nunique()["c"]
1789+
expected = Series(
1790+
[1, 1],
1791+
index=MultiIndex.from_arrays(
1792+
[CategoricalIndex([1, 2], name="a"), CategoricalIndex([1, 2], name="b")]
1793+
),
1794+
name="c",
1795+
)
1796+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)