From bbac28f6ebf35d3bcede2117554e1e3893f54c62 Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Mon, 18 Apr 2022 20:37:38 -0600 Subject: [PATCH] BUG: Add dropna to Grouper repr (#46754) --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/groupby/grouper.py | 7 +++---- pandas/core/resample.py | 9 +++------ pandas/tests/groupby/test_groupby.py | 2 +- pandas/tests/resample/test_time_grouper.py | 4 ++-- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 98d56bac402ac..36c06a5b56be3 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -603,6 +603,7 @@ Groupby/resample/rolling - Bug in :meth:`GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`) - Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`) - Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`) +- Bug in :meth:`Grouper.__repr__` where ``dropna`` was not included. Now it is (:issue:`46754`) Reshaping ^^^^^^^^^ diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 55c259b1e3d96..3f37c5f0c6df9 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -263,7 +263,7 @@ class Grouper: _gpr_index: Index | None _grouper: Index | None - _attributes: tuple[str, ...] = ("key", "level", "freq", "axis", "sort") + _attributes: tuple[str, ...] = ("key", "level", "freq", "axis", "sort", "dropna") def __new__(cls, *args, **kwargs): if kwargs.get("freq") is not None: @@ -287,6 +287,7 @@ def __init__( self.freq = freq self.axis = axis self.sort = sort + self.dropna = dropna self.grouper = None self._gpr_index = None @@ -295,7 +296,6 @@ def __init__( self.binner = None self._grouper = None self._indexer = None - self.dropna = dropna @final @property @@ -339,7 +339,7 @@ def _get_grouper( return self.binner, self.grouper, self.obj # type: ignore[return-value] @final - def _set_grouper(self, obj: NDFrame, sort: bool = False): + def _set_grouper(self, obj: NDFrame, sort: bool = False) -> None: """ given an object and the specifications, setup the internal grouper for this particular specification @@ -413,7 +413,6 @@ def _set_grouper(self, obj: NDFrame, sort: bool = False): # "NDFrameT", variable has type "None") self.obj = obj # type: ignore[assignment] self._gpr_index = ax - return self._gpr_index @final @property diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 209433a45f8b2..65adc68e39548 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1470,14 +1470,14 @@ def __init__( self, freq="Min", closed: Literal["left", "right"] | None = None, - label: str | None = None, + label: Literal["left", "right"] | None = None, how="mean", axis=0, fill_method=None, limit=None, loffset=None, kind: str | None = None, - convention: str | None = None, + convention: Literal["start", "end", "e", "s"] | None = None, base: int | None = None, origin: str | TimestampConvertibleTypes = "start_day", offset: TimedeltaConvertibleTypes | None = None, @@ -1523,10 +1523,7 @@ def __init__( self.closed = closed self.label = label self.kind = kind - - self.convention = convention or "E" - self.convention = self.convention.lower() - + self.convention = convention if convention is not None else "e" self.how = how self.fill_method = fill_method self.limit = limit diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 750ba802547ca..431c18ab6f4b2 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -31,7 +31,7 @@ def test_repr(): # GH18203 result = repr(Grouper(key="A", level="B")) - expected = "Grouper(key='A', level='B', axis=0, sort=False)" + expected = "Grouper(key='A', level='B', axis=0, sort=False, dropna=True)" assert result == expected diff --git a/pandas/tests/resample/test_time_grouper.py b/pandas/tests/resample/test_time_grouper.py index 8a94609900e1d..9f2635beaf1e7 100644 --- a/pandas/tests/resample/test_time_grouper.py +++ b/pandas/tests/resample/test_time_grouper.py @@ -266,7 +266,7 @@ def test_repr(): # GH18203 result = repr(Grouper(key="A", freq="H")) expected = ( - "TimeGrouper(key='A', freq=, axis=0, sort=True, " + "TimeGrouper(key='A', freq=, axis=0, sort=True, dropna=True, " "closed='left', label='left', how='mean', " "convention='e', origin='start_day')" ) @@ -274,7 +274,7 @@ def test_repr(): result = repr(Grouper(key="A", freq="H", origin="2000-01-01")) expected = ( - "TimeGrouper(key='A', freq=, axis=0, sort=True, " + "TimeGrouper(key='A', freq=, axis=0, sort=True, dropna=True, " "closed='left', label='left', how='mean', " "convention='e', origin=Timestamp('2000-01-01 00:00:00'))" )