Skip to content

REF: GroupBy.ops don't depend on mutation status #51102

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

Merged
merged 3 commits into from
Feb 1, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,6 @@ def _gotitem(self, key, ndim: int, subset=None):
sort=self.sort,
group_keys=self.group_keys,
observed=self.observed,
mutated=self.mutated,
dropna=self.dropna,
)
elif ndim == 1:
Expand Down
9 changes: 1 addition & 8 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@ class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
"group_keys",
"keys",
"level",
"mutated",
"obj",
"observed",
"sort",
Expand Down Expand Up @@ -900,7 +899,6 @@ def __init__(
sort: bool = True,
group_keys: bool | lib.NoDefault = True,
observed: bool = False,
mutated: bool = False,
dropna: bool = True,
) -> None:

Expand All @@ -919,7 +917,6 @@ def __init__(
self.sort = sort
self.group_keys = group_keys
self.observed = observed
self.mutated = mutated
self.dropna = dropna

if grouper is None:
Expand All @@ -930,7 +927,6 @@ def __init__(
level=level,
sort=sort,
observed=observed,
mutated=self.mutated,
dropna=self.dropna,
)

Expand Down Expand Up @@ -1491,7 +1487,7 @@ def _python_apply_general(
"""
values, mutated = self.grouper.apply(f, data, self.axis)
if not_indexed_same is None:
not_indexed_same = mutated or self.mutated
not_indexed_same = mutated

return self._wrap_applied_output(
data,
Expand Down Expand Up @@ -3113,7 +3109,6 @@ def _nth(
axis=self.axis,
level=self.level,
sort=self.sort,
mutated=self.mutated,
)

grb = dropped.groupby(
Expand Down Expand Up @@ -4263,7 +4258,6 @@ def get_groupby(
sort: bool = True,
group_keys: bool | lib.NoDefault = True,
observed: bool = False,
mutated: bool = False,
dropna: bool = True,
) -> GroupBy:

Expand Down Expand Up @@ -4291,7 +4285,6 @@ def get_groupby(
sort=sort,
group_keys=group_keys,
observed=observed,
mutated=mutated,
dropna=dropna,
)

Expand Down
5 changes: 1 addition & 4 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,6 @@ def get_grouper(
level=None,
sort: bool = True,
observed: bool = False,
mutated: bool = False,
validate: bool = True,
dropna: bool = True,
) -> tuple[ops.BaseGrouper, frozenset[Hashable], NDFrameT]:
Expand Down Expand Up @@ -957,9 +956,7 @@ def is_in_obj(gpr) -> bool:
groupings.append(Grouping(Index([], dtype="int"), np.array([], dtype=np.intp)))

# create the internals grouper
grouper = ops.BaseGrouper(
group_axis, groupings, sort=sort, mutated=mutated, dropna=dropna
)
grouper = ops.BaseGrouper(group_axis, groupings, sort=sort, dropna=dropna)
return grouper, frozenset(exclusions), obj


Expand Down
9 changes: 1 addition & 8 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@ class BaseGrouper:
sort : bool, default True
whether this grouper will give sorted result or not
group_keys : bool, default True
mutated : bool, default False
indexer : np.ndarray[np.intp], optional
the indexer created by Grouper
some groupers (TimeGrouper) will sort its axis and its
Expand All @@ -694,7 +693,6 @@ def __init__(
groupings: Sequence[grouper.Grouping],
sort: bool = True,
group_keys: bool = True,
mutated: bool = False,
indexer: npt.NDArray[np.intp] | None = None,
dropna: bool = True,
) -> None:
Expand All @@ -704,7 +702,6 @@ def __init__(
self._groupings: list[grouper.Grouping] = list(groupings)
self._sort = sort
self.group_keys = group_keys
self.mutated = mutated
self.indexer = indexer
self.dropna = dropna

Expand Down Expand Up @@ -772,7 +769,7 @@ def group_keys_seq(self):
def apply(
self, f: Callable, data: DataFrame | Series, axis: AxisInt = 0
) -> tuple[list, bool]:
mutated = self.mutated
mutated = False
splitter = self._get_splitter(data, axis=axis)
group_keys = self.group_keys_seq
result_values = []
Expand Down Expand Up @@ -1061,7 +1058,6 @@ class BinGrouper(BaseGrouper):
----------
bins : the split index of binlabels to group the item of axis
binlabels : the label list
mutated : bool, default False
indexer : np.ndarray[np.intp]

Examples
Expand All @@ -1084,18 +1080,15 @@ class BinGrouper(BaseGrouper):

bins: npt.NDArray[np.int64]
binlabels: Index
mutated: bool

def __init__(
self,
bins,
binlabels,
mutated: bool = False,
indexer=None,
) -> None:
self.bins = ensure_int64(bins)
self.binlabels = ensure_index(binlabels)
self.mutated = mutated
self.indexer = indexer

# These lengths must match, otherwise we could call agg_series
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,8 +1168,6 @@ def __init__(self, obj, parent=None, groupby=None, key=None, **kwargs) -> None:
self.key = key

self._groupby = groupby
self._groupby.mutated = True
self._groupby.grouper.mutated = True
self.groupby = copy.copy(parent.groupby)

@no_type_check
Expand Down
13 changes: 3 additions & 10 deletions pandas/tests/window/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,15 @@ def roll_frame():


class TestRolling:
def test_mutated(self, roll_frame):
def test_groupby_unsupported_argument(self, roll_frame):

msg = r"groupby\(\) got an unexpected keyword argument 'foo'"
with pytest.raises(TypeError, match=msg):
roll_frame.groupby("A", foo=1)

g = roll_frame.groupby("A")
assert not g.mutated
g = get_groupby(roll_frame, by="A", mutated=True)
assert g.mutated

def test_getitem(self, roll_frame):
g = roll_frame.groupby("A")
g_mutated = get_groupby(roll_frame, by="A", mutated=True)
g_mutated = get_groupby(roll_frame, by="A")

expected = g_mutated.B.apply(lambda x: x.rolling(2).mean())

Expand All @@ -80,7 +75,7 @@ def test_getitem_multiple(self, roll_frame):
# GH 13174
g = roll_frame.groupby("A")
r = g.rolling(2, min_periods=0)
g_mutated = get_groupby(roll_frame, by="A", mutated=True)
g_mutated = get_groupby(roll_frame, by="A")
expected = g_mutated.B.apply(lambda x: x.rolling(2, min_periods=0).count())

result = r.B.count()
Expand Down Expand Up @@ -789,8 +784,6 @@ def test_groupby_rolling_object_doesnt_affect_groupby_apply(self, roll_frame):
_ = g.rolling(window=4)
result = g.apply(lambda x: x.rolling(4).sum()).index
tm.assert_index_equal(result, expected)
assert not g.mutated
assert not g.grouper.mutated

@pytest.mark.parametrize(
("window", "min_periods", "closed", "expected"),
Expand Down