Skip to content

Commit fa6978b

Browse files
rhshadrachmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#49676: REGR: Remove groupby's __getattribute__ for nth
1 parent 136271a commit fa6978b

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
@@ -982,15 +982,6 @@ def __getattr__(self, attr: str):
982982
f"'{type(self).__name__}' object has no attribute '{attr}'"
983983
)
984984

985-
def __getattribute__(self, attr: str):
986-
# Intercept nth to allow both call and index
987-
if attr == "nth":
988-
return GroupByNthSelector(self)
989-
elif attr == "nth_actual":
990-
return super().__getattribute__("nth")
991-
else:
992-
return super().__getattribute__(attr)
993-
994985
@final
995986
def _make_wrapper(self, name: str) -> Callable:
996987
assert name in self._apply_allowlist
@@ -3016,13 +3007,10 @@ def backfill(self, limit=None):
30163007
return self.bfill(limit=limit)
30173008

30183009
@final
3010+
@property
30193011
@Substitution(name="groupby")
30203012
@Substitution(see_also=_common_see_also)
3021-
def nth(
3022-
self,
3023-
n: PositionalIndexer | tuple,
3024-
dropna: Literal["any", "all", None] = None,
3025-
) -> NDFrameT:
3013+
def nth(self) -> GroupByNthSelector:
30263014
"""
30273015
Take the nth row from each group if n is an int, otherwise a subset of rows.
30283016
@@ -3125,6 +3113,13 @@ def nth(
31253113
1 1 2.0
31263114
4 2 5.0
31273115
"""
3116+
return GroupByNthSelector(self)
3117+
3118+
def _nth(
3119+
self,
3120+
n: PositionalIndexer | tuple,
3121+
dropna: Literal["any", "all", None] = None,
3122+
) -> NDFrameT:
31283123
if not dropna:
31293124
with self._group_selection_context():
31303125
mask = self._make_mask_from_positional_indexer(n)

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)