Skip to content

Commit ebccd37

Browse files
authored
REF: GroupBy.ops don't depend on mutation status (#51102)
* REF: dont depend on self.mutated * REF/API: dont depend on mutation status
1 parent 4030432 commit ebccd37

File tree

6 files changed

+6
-33
lines changed

6 files changed

+6
-33
lines changed

pandas/core/groupby/generic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,6 @@ def _gotitem(self, key, ndim: int, subset=None):
17931793
sort=self.sort,
17941794
group_keys=self.group_keys,
17951795
observed=self.observed,
1796-
mutated=self.mutated,
17971796
dropna=self.dropna,
17981797
)
17991798
elif ndim == 1:

pandas/core/groupby/groupby.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,6 @@ class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
620620
"group_keys",
621621
"keys",
622622
"level",
623-
"mutated",
624623
"obj",
625624
"observed",
626625
"sort",
@@ -900,7 +899,6 @@ def __init__(
900899
sort: bool = True,
901900
group_keys: bool | lib.NoDefault = True,
902901
observed: bool = False,
903-
mutated: bool = False,
904902
dropna: bool = True,
905903
) -> None:
906904

@@ -919,7 +917,6 @@ def __init__(
919917
self.sort = sort
920918
self.group_keys = group_keys
921919
self.observed = observed
922-
self.mutated = mutated
923920
self.dropna = dropna
924921

925922
if grouper is None:
@@ -930,7 +927,6 @@ def __init__(
930927
level=level,
931928
sort=sort,
932929
observed=observed,
933-
mutated=self.mutated,
934930
dropna=self.dropna,
935931
)
936932

@@ -1491,7 +1487,7 @@ def _python_apply_general(
14911487
"""
14921488
values, mutated = self.grouper.apply(f, data, self.axis)
14931489
if not_indexed_same is None:
1494-
not_indexed_same = mutated or self.mutated
1490+
not_indexed_same = mutated
14951491

14961492
return self._wrap_applied_output(
14971493
data,
@@ -3114,7 +3110,6 @@ def _nth(
31143110
axis=self.axis,
31153111
level=self.level,
31163112
sort=self.sort,
3117-
mutated=self.mutated,
31183113
)
31193114

31203115
grb = dropped.groupby(
@@ -4264,7 +4259,6 @@ def get_groupby(
42644259
sort: bool = True,
42654260
group_keys: bool | lib.NoDefault = True,
42664261
observed: bool = False,
4267-
mutated: bool = False,
42684262
dropna: bool = True,
42694263
) -> GroupBy:
42704264

@@ -4292,7 +4286,6 @@ def get_groupby(
42924286
sort=sort,
42934287
group_keys=group_keys,
42944288
observed=observed,
4295-
mutated=mutated,
42964289
dropna=dropna,
42974290
)
42984291

pandas/core/groupby/grouper.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,6 @@ def get_grouper(
738738
level=None,
739739
sort: bool = True,
740740
observed: bool = False,
741-
mutated: bool = False,
742741
validate: bool = True,
743742
dropna: bool = True,
744743
) -> tuple[ops.BaseGrouper, frozenset[Hashable], NDFrameT]:
@@ -957,9 +956,7 @@ def is_in_obj(gpr) -> bool:
957956
groupings.append(Grouping(Index([], dtype="int"), np.array([], dtype=np.intp)))
958957

959958
# create the internals grouper
960-
grouper = ops.BaseGrouper(
961-
group_axis, groupings, sort=sort, mutated=mutated, dropna=dropna
962-
)
959+
grouper = ops.BaseGrouper(group_axis, groupings, sort=sort, dropna=dropna)
963960
return grouper, frozenset(exclusions), obj
964961

965962

pandas/core/groupby/ops.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ class BaseGrouper:
678678
sort : bool, default True
679679
whether this grouper will give sorted result or not
680680
group_keys : bool, default True
681-
mutated : bool, default False
682681
indexer : np.ndarray[np.intp], optional
683682
the indexer created by Grouper
684683
some groupers (TimeGrouper) will sort its axis and its
@@ -694,7 +693,6 @@ def __init__(
694693
groupings: Sequence[grouper.Grouping],
695694
sort: bool = True,
696695
group_keys: bool = True,
697-
mutated: bool = False,
698696
indexer: npt.NDArray[np.intp] | None = None,
699697
dropna: bool = True,
700698
) -> None:
@@ -704,7 +702,6 @@ def __init__(
704702
self._groupings: list[grouper.Grouping] = list(groupings)
705703
self._sort = sort
706704
self.group_keys = group_keys
707-
self.mutated = mutated
708705
self.indexer = indexer
709706
self.dropna = dropna
710707

@@ -772,7 +769,7 @@ def group_keys_seq(self):
772769
def apply(
773770
self, f: Callable, data: DataFrame | Series, axis: AxisInt = 0
774771
) -> tuple[list, bool]:
775-
mutated = self.mutated
772+
mutated = False
776773
splitter = self._get_splitter(data, axis=axis)
777774
group_keys = self.group_keys_seq
778775
result_values = []
@@ -1061,7 +1058,6 @@ class BinGrouper(BaseGrouper):
10611058
----------
10621059
bins : the split index of binlabels to group the item of axis
10631060
binlabels : the label list
1064-
mutated : bool, default False
10651061
indexer : np.ndarray[np.intp]
10661062
10671063
Examples
@@ -1084,18 +1080,15 @@ class BinGrouper(BaseGrouper):
10841080

10851081
bins: npt.NDArray[np.int64]
10861082
binlabels: Index
1087-
mutated: bool
10881083

10891084
def __init__(
10901085
self,
10911086
bins,
10921087
binlabels,
1093-
mutated: bool = False,
10941088
indexer=None,
10951089
) -> None:
10961090
self.bins = ensure_int64(bins)
10971091
self.binlabels = ensure_index(binlabels)
1098-
self.mutated = mutated
10991092
self.indexer = indexer
11001093

11011094
# These lengths must match, otherwise we could call agg_series

pandas/core/resample.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,8 +1168,6 @@ def __init__(self, obj, parent=None, groupby=None, key=None, **kwargs) -> None:
11681168
self.key = key
11691169

11701170
self._groupby = groupby
1171-
self._groupby.mutated = True
1172-
self._groupby.grouper.mutated = True
11731171
self.groupby = copy.copy(parent.groupby)
11741172

11751173
@no_type_check

pandas/tests/window/test_groupby.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,15 @@ def roll_frame():
4646

4747

4848
class TestRolling:
49-
def test_mutated(self, roll_frame):
49+
def test_groupby_unsupported_argument(self, roll_frame):
5050

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

55-
g = roll_frame.groupby("A")
56-
assert not g.mutated
57-
g = get_groupby(roll_frame, by="A", mutated=True)
58-
assert g.mutated
59-
6055
def test_getitem(self, roll_frame):
6156
g = roll_frame.groupby("A")
62-
g_mutated = get_groupby(roll_frame, by="A", mutated=True)
57+
g_mutated = get_groupby(roll_frame, by="A")
6358

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

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

8681
result = r.B.count()
@@ -789,8 +784,6 @@ def test_groupby_rolling_object_doesnt_affect_groupby_apply(self, roll_frame):
789784
_ = g.rolling(window=4)
790785
result = g.apply(lambda x: x.rolling(4).sum()).index
791786
tm.assert_index_equal(result, expected)
792-
assert not g.mutated
793-
assert not g.grouper.mutated
794787

795788
@pytest.mark.parametrize(
796789
("window", "min_periods", "closed", "expected"),

0 commit comments

Comments
 (0)