Skip to content

Commit b2b61af

Browse files
authored
REGR: Remove groupby's __getattribute__ for nth (#49676)
1 parent c7010a7 commit b2b61af

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

doc/source/whatsnew/v1.5.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Fixed regressions
1818
- Fixed regression in :meth:`DataFrame.plot` preventing :class:`~matplotlib.colors.Colormap` instance
1919
from being passed using the ``colormap`` argument if Matplotlib 3.6+ is used (:issue:`49374`)
2020
- Fixed regression in :func:`date_range` returning an invalid set of periods for ``CustomBusinessDay`` frequency and ``start`` date with timezone (:issue:`49441`)
21+
- Fixed performance regression in groupby operations (:issue:`49676`)
2122
-
2223

2324
.. ---------------------------------------------------------------------------

pandas/core/groupby/groupby.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -984,15 +984,6 @@ def __getattr__(self, attr: str):
984984
f"'{type(self).__name__}' object has no attribute '{attr}'"
985985
)
986986

987-
def __getattribute__(self, attr: str):
988-
# Intercept nth to allow both call and index
989-
if attr == "nth":
990-
return GroupByNthSelector(self)
991-
elif attr == "nth_actual":
992-
return super().__getattribute__("nth")
993-
else:
994-
return super().__getattribute__(attr)
995-
996987
@final
997988
def _op_via_apply(self, name: str, *args, **kwargs):
998989
"""Compute the result of an operation by using GroupBy's apply."""
@@ -2936,13 +2927,10 @@ def bfill(self, limit=None):
29362927
return self._fill("bfill", limit=limit)
29372928

29382929
@final
2930+
@property
29392931
@Substitution(name="groupby")
29402932
@Substitution(see_also=_common_see_also)
2941-
def nth(
2942-
self,
2943-
n: PositionalIndexer | tuple,
2944-
dropna: Literal["any", "all", None] = None,
2945-
) -> NDFrameT:
2933+
def nth(self) -> GroupByNthSelector:
29462934
"""
29472935
Take the nth row from each group if n is an int, otherwise a subset of rows.
29482936
@@ -3030,6 +3018,13 @@ def nth(
30303018
Columns: [A, B]
30313019
Index: []
30323020
"""
3021+
return GroupByNthSelector(self)
3022+
3023+
def _nth(
3024+
self,
3025+
n: PositionalIndexer | tuple,
3026+
dropna: Literal["any", "all", None] = None,
3027+
) -> NDFrameT:
30333028
if not dropna:
30343029
mask = self._make_mask_from_positional_indexer(n)
30353030

pandas/core/groupby/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def __call__(
297297
n: PositionalIndexer | tuple,
298298
dropna: Literal["any", "all", None] = None,
299299
) -> DataFrame | Series:
300-
return self.groupby_object.nth_actual(n, dropna)
300+
return self.groupby_object._nth(n, dropna)
301301

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

0 commit comments

Comments
 (0)