Skip to content

BUG: add reset logic for Grouper if new obj is passed in (#26564) #29800

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
9 changes: 9 additions & 0 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ def __init__(self, key=None, level=None, freq=None, axis=0, sort=False):
self.axis = axis
self.sort = sort

self._reset_grouper()

def _reset_grouper(self):
"""
Ensures Grouper object can be reused
See https://bit.ly/2D4Yd6V
"""
self.grouper = None
self.obj = None
self.indexer = None
Expand Down Expand Up @@ -155,6 +162,8 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
whether the resulting grouper should be sorted
"""
assert obj is not None
if not obj.equals(self.obj):
self._reset_grouper()

if self.key is not None and self.level is not None:
raise ValueError("The Grouper cannot specify both a key and a level!")
Expand Down