Skip to content

API: remove Grouper.groups #51170

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ Other API changes
- The levels of the index of the :class:`Series` returned from ``Series.sparse.from_coo`` now always have dtype ``int32``. Previously they had dtype ``int64`` (:issue:`50926`)
- :func:`to_datetime` with ``unit`` of either "Y" or "M" will now raise if a sequence contains a non-round ``float`` value, matching the ``Timestamp`` behavior (:issue:`50301`)
- The methods :meth:`Series.round`, :meth:`DataFrame.__invert__`, :meth:`Series.__invert__`, :meth:`DataFrame.swapaxes`, :meth:`DataFrame.first`, :meth:`DataFrame.last`, :meth:`Series.first`, :meth:`Series.last` and :meth:`DataFrame.align` will now always return new objects (:issue:`51032`)
- Removed :attr:`Grouper.groups`, which would have raised ``AttributeError`` if accessed (:issue:`51170`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_200.deprecations:
Expand Down
9 changes: 1 addition & 8 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ def __init__(
self.sort = sort
self.dropna = dropna

self.grouper = None
self._gpr_index = None
self.obj = None
self.indexer = None
Expand Down Expand Up @@ -328,7 +327,7 @@ def _set_grouper(self, obj: NDFrame, sort: bool = False) -> None:
if self.key is not None and self.level is not None:
raise ValueError("The Grouper cannot specify both a key and a level!")

# Keep self.grouper value before overriding
# Keep self._grouper value before overriding
if self._grouper is None:
# TODO: What are we assuming about subsequent calls?
self._grouper = self._gpr_index
Expand Down Expand Up @@ -387,12 +386,6 @@ def _set_grouper(self, obj: NDFrame, sort: bool = False) -> None:
self.obj = obj # type: ignore[assignment]
self._gpr_index = ax

@final
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah approved to quickly. I have a question:

df = DataFrame({"a": [1, 2, 3], "b": 1})
grper = pd.Grouper(key="a")
x = df.groupby(grper)
grper.groups

This returns the groups right now and wouldn't work anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this raises AttributeError on main

Copy link
Member

@phofl phofl Feb 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I wasn't on the newest commit, this changed in 385a667

Was this intentional? Should add a whatsnew then, since this is technically user facing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh your right, darn. i guess will have to revert part of #51145 and do that+this as a deprecation

@property
def groups(self):
# error: "None" has no attribute "groups"
return self.grouper.groups # type: ignore[attr-defined]

@final
def __repr__(self) -> str:
attrs_list = (
Expand Down