Skip to content

Commit 808e56e

Browse files
authored
DEPR: Grouper.grouper, Grouper.groups (#51182)
1 parent aebcfdf commit 808e56e

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,9 @@ Deprecations
782782
- Deprecated ``all`` and ``any`` reductions with ``datetime64`` and :class:`DatetimeTZDtype` dtypes, use e.g. ``(obj != pd.Timestamp(0), tz=obj.tz).all()`` instead (:issue:`34479`)
783783
- Deprecated unused arguments ``*args`` and ``**kwargs`` in :class:`Resampler` (:issue:`50977`)
784784
- Deprecated calling ``float`` or ``int`` on a single element :class:`Series` to return a ``float`` or ``int`` respectively. Extract the element before calling ``float`` or ``int`` instead (:issue:`51101`)
785+
- Deprecated :meth:`Grouper.groups`, use :meth:`Groupby.groups` instead (:issue:`51182`)
786+
- Deprecated :meth:`Grouper.grouper`, use :meth:`Groupby.grouper` instead (:issue:`51182`)
787+
-
785788

786789
.. ---------------------------------------------------------------------------
787790
.. _whatsnew_200.prior_deprecations:

pandas/core/groupby/grouper.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
cast,
1212
final,
1313
)
14+
import warnings
1415

1516
import numpy as np
1617

@@ -24,6 +25,7 @@
2425
)
2526
from pandas.errors import InvalidIndexError
2627
from pandas.util._decorators import cache_readonly
28+
from pandas.util._exceptions import find_stack_level
2729

2830
from pandas.core.dtypes.common import (
2931
is_categorical_dtype,
@@ -268,7 +270,7 @@ def __init__(
268270
self.sort = sort
269271
self.dropna = dropna
270272

271-
self.grouper = None
273+
self._grouper_deprecated = None
272274
self._gpr_index = None
273275
self.obj = None
274276
self.indexer = None
@@ -308,6 +310,10 @@ def _get_grouper(
308310
validate=validate,
309311
dropna=self.dropna,
310312
)
313+
# Without setting this, subsequent lookups to .groups raise
314+
# error: Incompatible types in assignment (expression has type "BaseGrouper",
315+
# variable has type "None")
316+
self._grouper_deprecated = grouper # type: ignore[assignment]
311317

312318
return grouper, obj
313319

@@ -328,7 +334,7 @@ def _set_grouper(self, obj: NDFrame, sort: bool = False) -> None:
328334
if self.key is not None and self.level is not None:
329335
raise ValueError("The Grouper cannot specify both a key and a level!")
330336

331-
# Keep self.grouper value before overriding
337+
# Keep self._grouper value before overriding
332338
if self._grouper is None:
333339
# TODO: What are we assuming about subsequent calls?
334340
self._grouper = self._gpr_index
@@ -387,11 +393,28 @@ def _set_grouper(self, obj: NDFrame, sort: bool = False) -> None:
387393
self.obj = obj # type: ignore[assignment]
388394
self._gpr_index = ax
389395

396+
@final
397+
@property
398+
def grouper(self):
399+
warnings.warn(
400+
f"{type(self).__name__}.grouper is deprecated and will be removed "
401+
"in a future version. Use GroupBy.grouper instead.",
402+
FutureWarning,
403+
stacklevel=find_stack_level(),
404+
)
405+
return self._grouper_deprecated
406+
390407
@final
391408
@property
392409
def groups(self):
410+
warnings.warn(
411+
f"{type(self).__name__}.groups is deprecated and will be removed "
412+
"in a future version. Use GroupBy.groups instead.",
413+
FutureWarning,
414+
stacklevel=find_stack_level(),
415+
)
393416
# error: "None" has no attribute "groups"
394-
return self.grouper.groups # type: ignore[attr-defined]
417+
return self._grouper_deprecated.groups # type: ignore[attr-defined]
395418

396419
@final
397420
def __repr__(self) -> str:

pandas/tests/groupby/test_grouping.py

+17
Original file line numberDiff line numberDiff line change
@@ -1049,3 +1049,20 @@ def test_grouping_by_key_is_in_axis():
10491049
result = gb.sum()
10501050
expected = DataFrame({"b": [1, 2], "c": [7, 5]})
10511051
tm.assert_frame_equal(result, expected)
1052+
1053+
1054+
def test_grouper_groups():
1055+
# GH#51182 check Grouper.groups does not raise AttributeError
1056+
df = DataFrame({"a": [1, 2, 3], "b": 1})
1057+
grper = Grouper(key="a")
1058+
gb = df.groupby(grper)
1059+
1060+
msg = "Use GroupBy.groups instead"
1061+
with tm.assert_produces_warning(FutureWarning, match=msg):
1062+
res = grper.groups
1063+
assert res is gb.groups
1064+
1065+
msg = "Use GroupBy.grouper instead"
1066+
with tm.assert_produces_warning(FutureWarning, match=msg):
1067+
res = grper.grouper
1068+
assert res is gb.grouper

0 commit comments

Comments
 (0)