Skip to content

Commit c2ef58e

Browse files
authored
DEPR: Properly enforce group_keys defaulting to False in resample (#52071)
1 parent 8e456d3 commit c2ef58e

File tree

9 files changed

+36
-45
lines changed

9 files changed

+36
-45
lines changed

Diff for: doc/source/user_guide/groupby.rst

+1-11
Original file line numberDiff line numberDiff line change
@@ -1241,18 +1241,8 @@ a common dtype will be determined in the same way as ``DataFrame`` construction.
12411241
Control grouped column(s) placement with ``group_keys``
12421242
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12431243

1244-
.. versionchanged:: 1.5.0
1245-
1246-
If ``group_keys=True`` is specified when calling :meth:`~DataFrame.groupby`,
1247-
functions passed to ``apply`` that return like-indexed outputs will have the
1248-
group keys added to the result index. Previous versions of pandas would add
1249-
the group keys only when the result from the applied function had a different
1250-
index than the input. If ``group_keys`` is not specified, the group keys will
1251-
not be added for like-indexed outputs. In the future this behavior
1252-
will change to always respect ``group_keys``, which defaults to ``True``.
1253-
12541244
To control whether the grouped column(s) are included in the indices, you can use
1255-
the argument ``group_keys``. Compare
1245+
the argument ``group_keys`` which defaults to ``True``. Compare
12561246

12571247
.. ipython:: python
12581248

Diff for: pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11427,7 +11427,7 @@ def resample(
1142711427
level: Level = None,
1142811428
origin: str | TimestampConvertibleTypes = "start_day",
1142911429
offset: TimedeltaConvertibleTypes | None = None,
11430-
group_keys: bool | lib.NoDefault = no_default,
11430+
group_keys: bool = False,
1143111431
) -> Resampler:
1143211432
return super().resample(
1143311433
rule=rule,

Diff for: pandas/core/generic.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -8564,7 +8564,7 @@ def resample(
85648564
level: Level = None,
85658565
origin: str | TimestampConvertibleTypes = "start_day",
85668566
offset: TimedeltaConvertibleTypes | None = None,
8567-
group_keys: bool_t | lib.NoDefault = lib.no_default,
8567+
group_keys: bool_t = False,
85688568
) -> Resampler:
85698569
"""
85708570
Resample time-series data.
@@ -8625,17 +8625,20 @@ def resample(
86258625
86268626
.. versionadded:: 1.1.0
86278627
8628-
group_keys : bool, optional
8628+
group_keys : bool, default False
86298629
Whether to include the group keys in the result index when using
8630-
``.apply()`` on the resampled object. Not specifying ``group_keys``
8631-
will retain values-dependent behavior from pandas 1.4
8632-
and earlier (see :ref:`pandas 1.5.0 Release notes
8633-
<whatsnew_150.enhancements.resample_group_keys>`
8634-
for examples). In a future version of pandas, the behavior will
8635-
default to the same as specifying ``group_keys=False``.
8630+
``.apply()`` on the resampled object.
86368631
86378632
.. versionadded:: 1.5.0
86388633
8634+
Not specifying ``group_keys`` will retain values-dependent behavior
8635+
from pandas 1.4 and earlier (see :ref:`pandas 1.5.0 Release notes
8636+
<whatsnew_150.enhancements.resample_group_keys>` for examples).
8637+
8638+
.. versionchanged:: 2.0.0
8639+
8640+
``group_keys`` now defaults to ``False``.
8641+
86398642
Returns
86408643
-------
86418644
pandas.core.Resampler

Diff for: pandas/core/groupby/groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
629629
grouper: ops.BaseGrouper
630630
keys: _KeysArgType | None = None
631631
level: IndexLabel | None = None
632-
group_keys: bool | lib.NoDefault
632+
group_keys: bool
633633

634634
@final
635635
def __len__(self) -> int:
@@ -919,7 +919,7 @@ def __init__(
919919
selection: IndexLabel | None = None,
920920
as_index: bool = True,
921921
sort: bool = True,
922-
group_keys: bool | lib.NoDefault = True,
922+
group_keys: bool = True,
923923
observed: bool | lib.NoDefault = lib.no_default,
924924
dropna: bool = True,
925925
) -> None:
@@ -4367,7 +4367,7 @@ def get_groupby(
43674367
by: _KeysArgType | None = None,
43684368
axis: AxisInt = 0,
43694369
grouper: ops.BaseGrouper | None = None,
4370-
group_keys: bool | lib.NoDefault = True,
4370+
group_keys: bool = True,
43714371
) -> GroupBy:
43724372
klass: type[GroupBy]
43734373
if isinstance(obj, Series):

Diff for: pandas/core/resample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def __init__(
153153
kind=None,
154154
*,
155155
gpr_index: Index,
156-
group_keys: bool | lib.NoDefault = lib.no_default,
156+
group_keys: bool = False,
157157
selection=None,
158158
) -> None:
159159
self._timegrouper = timegrouper
@@ -1585,7 +1585,7 @@ def __init__(
15851585
origin: Literal["epoch", "start", "start_day", "end", "end_day"]
15861586
| TimestampConvertibleTypes = "start_day",
15871587
offset: TimedeltaConvertibleTypes | None = None,
1588-
group_keys: bool | lib.NoDefault = True,
1588+
group_keys: bool = False,
15891589
**kwargs,
15901590
) -> None:
15911591
# Check for correctness of the keyword arguments which would

Diff for: pandas/core/series.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
properties,
3636
reshape,
3737
)
38-
from pandas._libs.lib import (
39-
is_range_indexer,
40-
no_default,
41-
)
38+
from pandas._libs.lib import is_range_indexer
4239
from pandas.compat import PYPY
4340
from pandas.compat.numpy import function as nv
4441
from pandas.errors import (
@@ -5582,7 +5579,7 @@ def resample(
55825579
level: Level = None,
55835580
origin: str | TimestampConvertibleTypes = "start_day",
55845581
offset: TimedeltaConvertibleTypes | None = None,
5585-
group_keys: bool | lib.NoDefault = no_default,
5582+
group_keys: bool = False,
55865583
) -> Resampler:
55875584
return super().resample(
55885585
rule=rule,

Diff for: pandas/core/shared_docs.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,24 @@
135135
Specifying ``sort=False`` with an ordered categorical grouper will no
136136
longer sort the values.
137137
138-
group_keys : bool, optional
138+
group_keys : bool, default True
139139
When calling apply and the ``by`` argument produces a like-indexed
140140
(i.e. :ref:`a transform <groupby.transform>`) result, add group keys to
141141
index to identify pieces. By default group keys are not included
142142
when the result's index (and column) labels match the inputs, and
143-
are included otherwise. This argument has no effect if the result produced
144-
is not like-indexed with respect to the input.
143+
are included otherwise.
145144
146145
.. versionchanged:: 1.5.0
147146
148-
Warns that `group_keys` will no longer be ignored when the
147+
Warns that ``group_keys`` will no longer be ignored when the
149148
result from ``apply`` is a like-indexed Series or DataFrame.
150149
Specify ``group_keys`` explicitly to include the group keys or
151150
not.
152151
152+
.. versionchanged:: 2.0.0
153+
154+
``group_keys`` now defaults to ``True``.
155+
153156
observed : bool, default False
154157
This only applies if any of the groupers are Categoricals.
155158
If True: only show observed values for categorical groupers.

Diff for: pandas/tests/resample/test_resample_api.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,22 @@ def test_resample_group_keys():
9696
df = DataFrame({"A": 1, "B": 2}, index=date_range("2000", periods=10))
9797
expected = df.copy()
9898

99-
# no warning
99+
# group_keys=False
100100
g = df.resample("5D", group_keys=False)
101-
with tm.assert_produces_warning(None):
102-
result = g.apply(lambda x: x)
101+
result = g.apply(lambda x: x)
103102
tm.assert_frame_equal(result, expected)
104103

105-
# no warning, group keys
106-
expected.index = pd.MultiIndex.from_arrays(
107-
[pd.to_datetime(["2000-01-01", "2000-01-06"]).repeat(5), expected.index]
108-
)
109-
104+
# group_keys defaults to False
110105
g = df.resample("5D")
111106
result = g.apply(lambda x: x)
112107
tm.assert_frame_equal(result, expected)
113108

109+
# group_keys=True
110+
expected.index = pd.MultiIndex.from_arrays(
111+
[pd.to_datetime(["2000-01-01", "2000-01-06"]).repeat(5), expected.index]
112+
)
114113
g = df.resample("5D", group_keys=True)
115-
with tm.assert_produces_warning(None):
116-
result = g.apply(lambda x: x)
114+
result = g.apply(lambda x: x)
117115
tm.assert_frame_equal(result, expected)
118116

119117

Diff for: pandas/tests/resample/test_resampler_grouper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def f(x):
280280
tm.assert_frame_equal(result, expected)
281281

282282
# A case for series
283-
expected = df["col1"].groupby(pd.Grouper(freq="M")).apply(f)
283+
expected = df["col1"].groupby(pd.Grouper(freq="M"), group_keys=False).apply(f)
284284
result = df["col1"].resample("M").apply(f)
285285
tm.assert_series_equal(result, expected)
286286

0 commit comments

Comments
 (0)