Skip to content
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

REGR: Remove groupby's __getattribute__ for nth #49676

Merged
merged 2 commits into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.2.rst
Expand Up @@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.plot` preventing :class:`~matplotlib.colors.Colormap` instance
from being passed using the ``colormap`` argument if Matplotlib 3.6+ is used (:issue:`49374`)
- Fixed regression in :func:`date_range` returning an invalid set of periods for ``CustomBusinessDay`` frequency and ``start`` date with timezone (:issue:`49441`)
- Fixed performance regression in groupby operations (:issue:`49676`)
-

.. ---------------------------------------------------------------------------
Expand Down
23 changes: 9 additions & 14 deletions pandas/core/groupby/groupby.py
Expand Up @@ -984,15 +984,6 @@ def __getattr__(self, attr: str):
f"'{type(self).__name__}' object has no attribute '{attr}'"
)

def __getattribute__(self, attr: str):
# Intercept nth to allow both call and index
if attr == "nth":
return GroupByNthSelector(self)
elif attr == "nth_actual":
return super().__getattribute__("nth")
else:
return super().__getattribute__(attr)

@final
def _op_via_apply(self, name: str, *args, **kwargs):
"""Compute the result of an operation by using GroupBy's apply."""
Expand Down Expand Up @@ -2936,13 +2927,10 @@ def bfill(self, limit=None):
return self._fill("bfill", limit=limit)

@final
@property
@Substitution(name="groupby")
@Substitution(see_also=_common_see_also)
def nth(
self,
n: PositionalIndexer | tuple,
dropna: Literal["any", "all", None] = None,
) -> NDFrameT:
def nth(self) -> GroupByNthSelector:
"""
Take the nth row from each group if n is an int, otherwise a subset of rows.

Expand Down Expand Up @@ -3030,6 +3018,13 @@ def nth(
Columns: [A, B]
Index: []
"""
return GroupByNthSelector(self)

def _nth(
self,
n: PositionalIndexer | tuple,
dropna: Literal["any", "all", None] = None,
) -> NDFrameT:
if not dropna:
mask = self._make_mask_from_positional_indexer(n)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/indexing.py
Expand Up @@ -297,7 +297,7 @@ def __call__(
n: PositionalIndexer | tuple,
dropna: Literal["any", "all", None] = None,
) -> DataFrame | Series:
return self.groupby_object.nth_actual(n, dropna)
return self.groupby_object._nth(n, dropna)

def __getitem__(self, n: PositionalIndexer | tuple) -> DataFrame | Series:
return self.groupby_object.nth_actual(n)
return self.groupby_object._nth(n)