Skip to content

Commit a7f1ead

Browse files
committed
Deprecate mutated
1 parent 803ed0b commit a7f1ead

File tree

7 files changed

+63
-22
lines changed

7 files changed

+63
-22
lines changed

doc/source/whatsnew/v1.0.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ Deprecations
215215
value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)``
216216
is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`).
217217
- :func:`is_extension_type` is deprecated, :func:`is_extension_array_dtype` should be used instead (:issue:`29457`)
218-
218+
- :meth:`DataFrame.groupby` and :meth:`Series.groupby` accepted a ``mutated`` parameter, which is actually
219+
an implementation detail. It has been deprecated in the public interface (:issue:`29511`).
219220

220221
.. _whatsnew_1000.prior_deprecations:
221222

pandas/core/generic.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -7875,8 +7875,10 @@ def groupby(
78757875
If False: show all values for categorical groupers.
78767876
78777877
.. versionadded:: 0.23.0
7878-
mutated : bool, False
7879-
`mutated` is passed to groupby.
7878+
mutated : bool, default False
7879+
.. deprecated:: 1.0
7880+
This is an internal parameter and will be removed from the public
7881+
interface in the future
78807882
78817883
Returns
78827884
-------
@@ -7939,12 +7941,18 @@ def groupby(
79397941
Captive 210.0
79407942
Wild 185.0
79417943
"""
7942-
from pandas.core.groupby.groupby import groupby
7944+
from pandas.core.groupby.groupby import get_groupby
79437945

79447946
if level is None and by is None:
79457947
raise TypeError("You have to supply one of 'by' and 'level'")
79467948
axis = self._get_axis_number(axis)
7947-
return groupby(
7949+
7950+
if mutated is not False:
7951+
warnings.warn(
7952+
"Parameter 'mutated' is deprecated", FutureWarning, stacklevel=2
7953+
)
7954+
7955+
return get_groupby(
79487956
self,
79497957
by=by,
79507958
axis=axis,

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
GroupBy,
6363
_apply_docs,
6464
_transform_template,
65-
groupby,
65+
get_groupby,
6666
)
6767
from pandas.core.index import Index, MultiIndex, _all_indexes_same
6868
import pandas.core.indexes.base as ibase
@@ -997,7 +997,7 @@ def _cython_agg_blocks(
997997
# reductions; see GH#28949
998998
obj = obj.iloc[:, 0]
999999

1000-
s = groupby(obj, self.grouper)
1000+
s = get_groupby(obj, self.grouper)
10011001
try:
10021002
result = s.aggregate(lambda x: alt(x, axis=self.axis))
10031003
except TypeError:

pandas/core/groupby/groupby.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -2486,18 +2486,42 @@ def _reindex_output(self, output):
24862486

24872487

24882488
@Appender(GroupBy.__doc__)
2489-
def groupby(obj: NDFrame, by, **kwds):
2490-
if isinstance(obj, Series):
2491-
from pandas.core.groupby.generic import SeriesGroupBy
2489+
def get_groupby(
2490+
obj: NDFrame,
2491+
by=None,
2492+
axis: int = 0,
2493+
level=None,
2494+
grouper=None,
2495+
exclusions=None,
2496+
selection=None,
2497+
as_index: bool = True,
2498+
sort: bool = True,
2499+
group_keys: bool = True,
2500+
squeeze: bool = False,
2501+
observed: bool = False,
2502+
mutated: bool = False,
2503+
):
2504+
from pandas.core.groupby.generic import DataFrameGroupBy, SeriesGroupBy
24922505

2493-
klass = (
2494-
SeriesGroupBy
2495-
) # type: Union[Type["SeriesGroupBy"], Type["DataFrameGroupBy"]]
2506+
if isinstance(obj, Series):
2507+
klass = SeriesGroupBy
24962508
elif isinstance(obj, DataFrame):
2497-
from pandas.core.groupby.generic import DataFrameGroupBy
2498-
24992509
klass = DataFrameGroupBy
25002510
else:
25012511
raise TypeError("invalid type: {obj}".format(obj=obj))
25022512

2503-
return klass(obj, by, **kwds)
2513+
return klass(
2514+
obj=obj,
2515+
keys=by,
2516+
axis=axis,
2517+
level=level,
2518+
grouper=grouper,
2519+
exclusions=exclusions,
2520+
selection=selection,
2521+
as_index=as_index,
2522+
sort=sort,
2523+
group_keys=group_keys,
2524+
squeeze=squeeze,
2525+
observed=observed,
2526+
mutated=mutated,
2527+
)

pandas/core/resample.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pandas.core.generic import _shared_docs
2222
from pandas.core.groupby.base import GroupByMixin
2323
from pandas.core.groupby.generic import SeriesGroupBy
24-
from pandas.core.groupby.groupby import GroupBy, _GroupBy, _pipe_template, groupby
24+
from pandas.core.groupby.groupby import GroupBy, _GroupBy, _pipe_template, get_groupby
2525
from pandas.core.groupby.grouper import Grouper
2626
from pandas.core.groupby.ops import BinGrouper
2727
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
@@ -334,7 +334,7 @@ def _gotitem(self, key, ndim, subset=None):
334334
grouper = self.grouper
335335
if subset is None:
336336
subset = self.obj
337-
grouped = groupby(subset, by=None, grouper=grouper, axis=self.axis)
337+
grouped = get_groupby(subset, by=None, grouper=grouper, axis=self.axis)
338338

339339
# try the key selection
340340
try:
@@ -353,7 +353,7 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
353353

354354
obj = self._selected_obj
355355

356-
grouped = groupby(obj, by=None, grouper=grouper, axis=self.axis)
356+
grouped = get_groupby(obj, by=None, grouper=grouper, axis=self.axis)
357357

358358
try:
359359
if isinstance(obj, ABCDataFrame) and callable(how):

pandas/tests/groupby/test_groupby.py

+7
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,13 @@ def test_no_mutate_but_looks_like():
953953
tm.assert_series_equal(result1, result2)
954954

955955

956+
def test_mutated_deprecated():
957+
# GH-29511
958+
df = DataFrame({"key": [1, 1, 1, 2, 2, 2, 3, 3, 3], "value": range(9)})
959+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=True):
960+
df.groupby("key", mutated=True)
961+
962+
956963
def test_groupby_series_indexed_differently():
957964
s1 = Series(
958965
[5.0, -9.0, 4.0, 100.0, -5.0, 55.0, 6.7],

pandas/tests/window/test_grouper.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pandas as pd
55
from pandas import DataFrame, Series
6+
from pandas.core.groupby.groupby import get_groupby
67
import pandas.util.testing as tm
78

89

@@ -19,12 +20,12 @@ def test_mutated(self):
1920

2021
g = self.frame.groupby("A")
2122
assert not g.mutated
22-
g = self.frame.groupby("A", mutated=True)
23+
g = get_groupby(self.frame, by="A", mutated=True)
2324
assert g.mutated
2425

2526
def test_getitem(self):
2627
g = self.frame.groupby("A")
27-
g_mutated = self.frame.groupby("A", mutated=True)
28+
g_mutated = get_groupby(self.frame, by="A", mutated=True)
2829

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

@@ -45,7 +46,7 @@ def test_getitem_multiple(self):
4546
# GH 13174
4647
g = self.frame.groupby("A")
4748
r = g.rolling(2)
48-
g_mutated = self.frame.groupby("A", mutated=True)
49+
g_mutated = get_groupby(self.frame, by="A", mutated=True)
4950
expected = g_mutated.B.apply(lambda x: x.rolling(2).count())
5051

5152
result = r.B.count()

0 commit comments

Comments
 (0)