From f1ec0e59bcb40c5d31f5f6ad63de2eaaab218676 Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Mon, 7 Mar 2022 20:53:47 +0000 Subject: [PATCH 1/4] TYP: Add Literal annotations to TimeGrouper This doesn't affect the behaviour, since the possible values are already restricted a few lines above this. --- pandas/core/resample.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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 From f9cdbc6837365ff1626a1eb533602dd67d6f70a9 Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Mon, 7 Mar 2022 21:02:08 +0000 Subject: [PATCH 2/4] TYP: Fix annotation of Grouper._set_grouper The return value is never used. Don't pretend that it is. Ideally we don't even hold this state inside the Grouper, it would make it easier to reason about. But just do this tiny change for now, still an improvement. See https://github.com/pandas-dev/pandas/pull/46258 for more details. --- pandas/core/groupby/grouper.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index 55c259b1e3d96..b541ea1d5127a 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -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 From 3249b4351743dd967947307789503d4b650f107d Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Mon, 7 Mar 2022 22:36:05 +0000 Subject: [PATCH 3/4] BUG: Include "dropna" in Grouper._attributes All this changes I think is that now `dropna` is included in the `__repr__` output. See https://github.com/pandas-dev/pandas/pull/46258 for more discussion. And move dropna up out of the "generated" attribute section into the "source" attribute section in the __init__ --- doc/source/whatsnew/v1.4.3.rst | 2 +- pandas/core/groupby/grouper.py | 4 ++-- pandas/tests/groupby/test_groupby.py | 2 +- pandas/tests/resample/test_time_grouper.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 0c326e15d90ed..83280d46e0c3b 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -24,7 +24,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- +- Include ``dropna`` in ``__repr__`` of a :class:`Grouper` (:issue:`46754`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index b541ea1d5127a..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 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'))" ) From dcdb7c7403bd14032788c31c5dfe37f81b6188ad Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Sun, 17 Apr 2022 02:21:16 +0000 Subject: [PATCH 4/4] Fixup: Move whatsnew note to the right release --- doc/source/whatsnew/v1.4.3.rst | 2 +- doc/source/whatsnew/v1.5.0.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 83280d46e0c3b..0c326e15d90ed 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -24,7 +24,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- Include ``dropna`` in ``__repr__`` of a :class:`Grouper` (:issue:`46754`) +- - .. --------------------------------------------------------------------------- diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 358d9447b131d..47028230ad103 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -599,6 +599,7 @@ Groupby/resample/rolling - Bug in :meth:`GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`) - 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:`Grouper.__repr__` where ``dropna`` was not included. Now it is. (:issue:`46754`) - Reshaping