Skip to content

Commit 6093074

Browse files
authored
DEPR: kind keyword in resample (#55895)
* DEPR: kind keyword in resample * GH ref * update usage
1 parent e2aa710 commit 6093074

File tree

5 files changed

+78
-21
lines changed

5 files changed

+78
-21
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ Other Deprecations
399399
- Deprecated the :class:`.Grouping` attributes ``group_index``, ``result_index``, and ``group_arraylike``; these will be removed in a future version of pandas (:issue:`56148`)
400400
- Deprecated the ``errors="ignore"`` option in :func:`to_datetime`, :func:`to_timedelta`, and :func:`to_numeric`; explicitly catch exceptions instead (:issue:`54467`)
401401
- Deprecated the ``fastpath`` keyword in the :class:`Series` constructor (:issue:`20110`)
402+
- Deprecated the ``kind`` keyword in :meth:`Series.resample` and :meth:`DataFrame.resample`, explicitly cast the object's ``index`` instead (:issue:`55895`)
402403
- Deprecated the ``ordinal`` keyword in :class:`PeriodIndex`, use :meth:`PeriodIndex.from_ordinals` instead (:issue:`55960`)
403404
- Deprecated the behavior of :meth:`Series.value_counts` and :meth:`Index.value_counts` with object dtype; in a future version these will not perform dtype inference on the resulting :class:`Index`, do ``result.index = result.index.infer_objects()`` to retain the old behavior (:issue:`56161`)
404405
- Deprecated the extension test classes ``BaseNoReduceTests``, ``BaseBooleanReduceTests``, and ``BaseNumericReduceTests``, use ``BaseReduceTests`` instead (:issue:`54663`)

pandas/core/generic.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -9242,7 +9242,7 @@ def resample(
92429242
closed: Literal["right", "left"] | None = None,
92439243
label: Literal["right", "left"] | None = None,
92449244
convention: Literal["start", "end", "s", "e"] = "start",
9245-
kind: Literal["timestamp", "period"] | None = None,
9245+
kind: Literal["timestamp", "period"] | None | lib.NoDefault = lib.no_default,
92469246
on: Level | None = None,
92479247
level: Level | None = None,
92489248
origin: str | TimestampConvertibleTypes = "start_day",
@@ -9284,6 +9284,9 @@ def resample(
92849284
`DateTimeIndex` or 'period' to convert it to a `PeriodIndex`.
92859285
By default the input representation is retained.
92869286
9287+
.. deprecated:: 2.2.0
9288+
Convert index to desired type explicitly instead.
9289+
92879290
on : str, optional
92889291
For a DataFrame, column to use instead of index for resampling.
92899292
Column must be datetime-like.
@@ -9641,6 +9644,18 @@ def resample(
96419644
else:
96429645
axis = 0
96439646

9647+
if kind is not lib.no_default:
9648+
# GH#55895
9649+
warnings.warn(
9650+
f"The 'kind' keyword in {type(self).__name__}.resample is "
9651+
"deprecated and will be removed in a future version. "
9652+
"Explicitly cast the index to the desired type instead",
9653+
FutureWarning,
9654+
stacklevel=find_stack_level(),
9655+
)
9656+
else:
9657+
kind = None
9658+
96449659
return get_resampler(
96459660
cast("Series | DataFrame", self),
96469661
freq=rule,

pandas/tests/io/excel/test_writers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,9 @@ def test_to_excel_timedelta(self, path):
771771
tm.assert_frame_equal(expected, recons)
772772

773773
def test_to_excel_periodindex(self, path):
774-
xp = tm.makeTimeDataFrame()[:5].resample("ME", kind="period").mean()
774+
# xp has a PeriodIndex
775+
df = tm.makeTimeDataFrame()[:5]
776+
xp = df.resample("ME").mean().to_period("M")
775777

776778
xp.to_excel(path, sheet_name="sht1")
777779

pandas/tests/resample/test_datetime_index.py

+26-8
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,9 @@ def test_resample_frame_basic_M_A(freq, unit):
455455
def test_resample_frame_basic_kind(freq, unit):
456456
df = tm.makeTimeDataFrame()
457457
df.index = df.index.as_unit(unit)
458-
df.resample(freq, kind="period").mean()
458+
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
459+
with tm.assert_produces_warning(FutureWarning, match=msg):
460+
df.resample(freq, kind="period").mean()
459461

460462

461463
def test_resample_upsample(unit):
@@ -687,7 +689,9 @@ def test_resample_timestamp_to_period(
687689
ts = simple_date_range_series("1/1/1990", "1/1/2000")
688690
ts.index = ts.index.as_unit(unit)
689691

690-
result = ts.resample(freq, kind="period").mean()
692+
msg = "The 'kind' keyword in Series.resample is deprecated"
693+
with tm.assert_produces_warning(FutureWarning, match=msg):
694+
result = ts.resample(freq, kind="period").mean()
691695
expected = ts.resample(freq).mean()
692696
expected.index = period_range(**expected_kwargs)
693697
tm.assert_series_equal(result, expected)
@@ -1020,7 +1024,9 @@ def test_resample_to_period_monthly_buglet(unit):
10201024
rng = date_range("1/1/2000", "12/31/2000").as_unit(unit)
10211025
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
10221026

1023-
result = ts.resample("ME", kind="period").mean()
1027+
msg = "The 'kind' keyword in Series.resample is deprecated"
1028+
with tm.assert_produces_warning(FutureWarning, match=msg):
1029+
result = ts.resample("ME", kind="period").mean()
10241030
exp_index = period_range("Jan-2000", "Dec-2000", freq="M")
10251031
tm.assert_index_equal(result.index, exp_index)
10261032

@@ -1139,14 +1145,18 @@ def test_resample_anchored_intraday(unit):
11391145
df = DataFrame(rng.month, index=rng)
11401146

11411147
result = df.resample("ME").mean()
1142-
expected = df.resample("ME", kind="period").mean().to_timestamp(how="end")
1148+
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
1149+
with tm.assert_produces_warning(FutureWarning, match=msg):
1150+
expected = df.resample("ME", kind="period").mean().to_timestamp(how="end")
11431151
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
11441152
expected.index = expected.index.as_unit(unit)._with_freq("infer")
11451153
assert expected.index.freq == "ME"
11461154
tm.assert_frame_equal(result, expected)
11471155

11481156
result = df.resample("ME", closed="left").mean()
1149-
exp = df.shift(1, freq="D").resample("ME", kind="period").mean()
1157+
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
1158+
with tm.assert_produces_warning(FutureWarning, match=msg):
1159+
exp = df.shift(1, freq="D").resample("ME", kind="period").mean()
11501160
exp = exp.to_timestamp(how="end")
11511161

11521162
exp.index = exp.index + Timedelta(1, "ns") - Timedelta(1, "D")
@@ -1160,15 +1170,21 @@ def test_resample_anchored_intraday2(unit):
11601170
df = DataFrame(rng.month, index=rng)
11611171

11621172
result = df.resample("QE").mean()
1163-
expected = df.resample("QE", kind="period").mean().to_timestamp(how="end")
1173+
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
1174+
with tm.assert_produces_warning(FutureWarning, match=msg):
1175+
expected = df.resample("QE", kind="period").mean().to_timestamp(how="end")
11641176
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
11651177
expected.index._data.freq = "QE"
11661178
expected.index._freq = lib.no_default
11671179
expected.index = expected.index.as_unit(unit)
11681180
tm.assert_frame_equal(result, expected)
11691181

11701182
result = df.resample("QE", closed="left").mean()
1171-
expected = df.shift(1, freq="D").resample("QE", kind="period", closed="left").mean()
1183+
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
1184+
with tm.assert_produces_warning(FutureWarning, match=msg):
1185+
expected = (
1186+
df.shift(1, freq="D").resample("QE", kind="period", closed="left").mean()
1187+
)
11721188
expected = expected.to_timestamp(how="end")
11731189
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
11741190
expected.index._data.freq = "QE"
@@ -1225,7 +1241,9 @@ def test_corner_cases_date(simple_date_range_series, unit):
12251241
# resample to periods
12261242
ts = simple_date_range_series("2000-04-28", "2000-04-30 11:00", freq="h")
12271243
ts.index = ts.index.as_unit(unit)
1228-
result = ts.resample("ME", kind="period").mean()
1244+
msg = "The 'kind' keyword in Series.resample is deprecated"
1245+
with tm.assert_produces_warning(FutureWarning, match=msg):
1246+
result = ts.resample("ME", kind="period").mean()
12291247
assert len(result) == 1
12301248
assert result.index[0] == Period("2000-04", freq="M")
12311249

pandas/tests/resample/test_period_index.py

+32-11
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def test_asfreq(self, series_and_frame, freq, kind):
7777
end = (obj.index[-1] + obj.index.freq).to_timestamp(how="start")
7878
new_index = date_range(start=start, end=end, freq=freq, inclusive="left")
7979
expected = obj.to_timestamp().reindex(new_index).to_period(freq)
80-
result = obj.resample(freq, kind=kind).asfreq()
80+
msg = "The 'kind' keyword in (Series|DataFrame).resample is deprecated"
81+
with tm.assert_produces_warning(FutureWarning, match=msg):
82+
result = obj.resample(freq, kind=kind).asfreq()
8183
tm.assert_almost_equal(result, expected)
8284

8385
def test_asfreq_fill_value(self, series):
@@ -90,7 +92,9 @@ def test_asfreq_fill_value(self, series):
9092
freq="1h",
9193
)
9294
expected = s.to_timestamp().reindex(new_index, fill_value=4.0)
93-
result = s.resample("1h", kind="timestamp").asfreq(fill_value=4.0)
95+
msg = "The 'kind' keyword in Series.resample is deprecated"
96+
with tm.assert_produces_warning(FutureWarning, match=msg):
97+
result = s.resample("1h", kind="timestamp").asfreq(fill_value=4.0)
9498
tm.assert_series_equal(result, expected)
9599

96100
frame = s.to_frame("value")
@@ -100,7 +104,9 @@ def test_asfreq_fill_value(self, series):
100104
freq="1h",
101105
)
102106
expected = frame.to_timestamp().reindex(new_index, fill_value=3.0)
103-
result = frame.resample("1h", kind="timestamp").asfreq(fill_value=3.0)
107+
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
108+
with tm.assert_produces_warning(FutureWarning, match=msg):
109+
result = frame.resample("1h", kind="timestamp").asfreq(fill_value=3.0)
104110
tm.assert_frame_equal(result, expected)
105111

106112
@pytest.mark.parametrize("freq", ["h", "12h", "2D", "W"])
@@ -119,8 +125,10 @@ def test_selection(self, index, freq, kind, kwargs):
119125
r"not currently supported, use \.set_index\(\.\.\.\) to "
120126
"explicitly set index"
121127
)
128+
depr_msg = "The 'kind' keyword in DataFrame.resample is deprecated"
122129
with pytest.raises(NotImplementedError, match=msg):
123-
df.resample(freq, kind=kind, **kwargs)
130+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
131+
df.resample(freq, kind=kind, **kwargs)
124132

125133
@pytest.mark.parametrize("month", MONTHS)
126134
@pytest.mark.parametrize("meth", ["ffill", "bfill"])
@@ -250,9 +258,12 @@ def test_resample_basic(self):
250258
name="idx",
251259
)
252260
expected = Series([34.5, 79.5], index=index)
253-
result = s.to_period().resample("min", kind="period").mean()
261+
msg = "The 'kind' keyword in Series.resample is deprecated"
262+
with tm.assert_produces_warning(FutureWarning, match=msg):
263+
result = s.to_period().resample("min", kind="period").mean()
254264
tm.assert_series_equal(result, expected)
255-
result2 = s.resample("min", kind="period").mean()
265+
with tm.assert_produces_warning(FutureWarning, match=msg):
266+
result2 = s.resample("min", kind="period").mean()
256267
tm.assert_series_equal(result2, expected)
257268

258269
@pytest.mark.parametrize(
@@ -307,7 +318,9 @@ def test_with_local_timezone(self, tz):
307318

308319
series = Series(1, index=index)
309320
series = series.tz_convert(local_timezone)
310-
result = series.resample("D", kind="period").mean()
321+
msg = "The 'kind' keyword in Series.resample is deprecated"
322+
with tm.assert_produces_warning(FutureWarning, match=msg):
323+
result = series.resample("D", kind="period").mean()
311324

312325
# Create the expected series
313326
# Index is moved back a day with the timezone conversion from UTC to
@@ -406,7 +419,9 @@ def test_weekly_upsample(self, day, target, convention, simple_period_range_seri
406419
def test_resample_to_timestamps(self, simple_period_range_series):
407420
ts = simple_period_range_series("1/1/1990", "12/31/1995", freq="M")
408421

409-
result = ts.resample("Y-DEC", kind="timestamp").mean()
422+
msg = "The 'kind' keyword in Series.resample is deprecated"
423+
with tm.assert_produces_warning(FutureWarning, match=msg):
424+
result = ts.resample("Y-DEC", kind="timestamp").mean()
410425
expected = ts.to_timestamp(how="start").resample("YE-DEC").mean()
411426
tm.assert_series_equal(result, expected)
412427

@@ -466,7 +481,9 @@ def test_resample_5minute(self, freq, kind):
466481
expected = ts.to_timestamp().resample(freq).mean()
467482
if kind != "timestamp":
468483
expected = expected.to_period(freq)
469-
result = ts.resample(freq, kind=kind).mean()
484+
msg = "The 'kind' keyword in Series.resample is deprecated"
485+
with tm.assert_produces_warning(FutureWarning, match=msg):
486+
result = ts.resample(freq, kind=kind).mean()
470487
tm.assert_series_equal(result, expected)
471488

472489
def test_upsample_daily_business_daily(self, simple_period_range_series):
@@ -538,7 +555,9 @@ def test_resample_tz_localized2(self):
538555
tm.assert_series_equal(result, expected)
539556

540557
# for good measure
541-
result = s.resample("D", kind="period").mean()
558+
msg = "The 'kind' keyword in Series.resample is deprecated"
559+
with tm.assert_produces_warning(FutureWarning, match=msg):
560+
result = s.resample("D", kind="period").mean()
542561
ex_index = period_range("2001-09-20", periods=1, freq="D")
543562
expected = Series([1.5], index=ex_index)
544563
tm.assert_series_equal(result, expected)
@@ -783,7 +802,9 @@ def test_upsampling_ohlc(self, freq, period_mult, kind):
783802
# of the last original period, so extend accordingly:
784803
new_index = period_range(start="2000", freq=freq, periods=period_mult * len(pi))
785804
expected = expected.reindex(new_index)
786-
result = s.resample(freq, kind=kind).ohlc()
805+
msg = "The 'kind' keyword in Series.resample is deprecated"
806+
with tm.assert_produces_warning(FutureWarning, match=msg):
807+
result = s.resample(freq, kind=kind).ohlc()
787808
tm.assert_frame_equal(result, expected)
788809

789810
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)