Skip to content
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

DEPR: kind keyword in resample #55895

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Other Deprecations
- Deprecated strings ``T``, ``S``, ``L``, ``U``, and ``N`` denoting frequencies in :class:`Minute`, :class:`Second`, :class:`Milli`, :class:`Micro`, :class:`Nano` (:issue:`52536`)
- Deprecated the ``errors="ignore"`` option in :func:`to_datetime`, :func:`to_timedelta`, and :func:`to_numeric`; explicitly catch exceptions instead (:issue:`54467`)
- Deprecated the ``fastpath`` keyword in the :class:`Series` constructor (:issue:`20110`)
- Deprecated the ``kind`` keyword in :meth:`Series.resample` and :meth:`DataFrame.resample`, explicitly cast the object's ``index`` instead (:issue:`55895`)
- Deprecated the extension test classes ``BaseNoReduceTests``, ``BaseBooleanReduceTests``, and ``BaseNumericReduceTests``, use ``BaseReduceTests`` instead (:issue:`54663`)
- Deprecated the option ``mode.data_manager`` and the ``ArrayManager``; only the ``BlockManager`` will be available in future versions (:issue:`55043`)
- Deprecated the previous implementation of :class:`DataFrame.stack`; specify ``future_stack=True`` to adopt the future version (:issue:`53515`)
Expand Down
17 changes: 16 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9171,7 +9171,7 @@ def resample(
closed: Literal["right", "left"] | None = None,
label: Literal["right", "left"] | None = None,
convention: Literal["start", "end", "s", "e"] = "start",
kind: Literal["timestamp", "period"] | None = None,
kind: Literal["timestamp", "period"] | None | lib.NoDefault = lib.no_default,
on: Level | None = None,
level: Level | None = None,
origin: str | TimestampConvertibleTypes = "start_day",
Expand Down Expand Up @@ -9213,6 +9213,9 @@ def resample(
`DateTimeIndex` or 'period' to convert it to a `PeriodIndex`.
By default the input representation is retained.

.. deprecated:: 2.2.0
Convert index to desired type explicitly instead.

on : str, optional
For a DataFrame, column to use instead of index for resampling.
Column must be datetime-like.
Expand Down Expand Up @@ -9570,6 +9573,18 @@ def resample(
else:
axis = 0

if kind is not lib.no_default:
# GH#55895
warnings.warn(
f"The 'kind' keyword in {type(self).__name__}.resample is "
"deprecated and will be removed in a future version. "
"Explicitly cast the index to the desired type instead",
FutureWarning,
stacklevel=find_stack_level(),
)
else:
kind = None

return get_resampler(
cast("Series | DataFrame", self),
freq=rule,
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ def test_to_excel_timedelta(self, path):
tm.assert_frame_equal(expected, recons)

def test_to_excel_periodindex(self, tsframe, path):
xp = tsframe.resample("ME", kind="period").mean()
# xp has a PeriodIndex
xp = tsframe.resample("ME").mean().to_period("M")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter at all that the order of operations here change? Before the conversion to period happened before the mean() call - wonder if there are differences in how those objects implement mean that could cause rounding issues or behavior changes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the only thing we care about is the xp we get here, since we are checking that it round-trips. i checked manually that this is equivalent to what we have in main


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

Expand Down
34 changes: 26 additions & 8 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ def test_resample_frame_basic_M_A(freq, unit):
def test_resample_frame_basic_kind(freq, unit):
df = tm.makeTimeDataFrame()
df.index = df.index.as_unit(unit)
df.resample(freq, kind="period").mean()
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.resample(freq, kind="period").mean()


def test_resample_upsample(unit):
Expand Down Expand Up @@ -674,7 +676,9 @@ def test_resample_timestamp_to_period(
ts = simple_date_range_series("1/1/1990", "1/1/2000")
ts.index = ts.index.as_unit(unit)

result = ts.resample(freq, kind="period").mean()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ts.resample(freq, kind="period").mean()
expected = ts.resample(freq).mean()
expected.index = period_range(**expected_kwargs)
tm.assert_series_equal(result, expected)
Expand Down Expand Up @@ -1007,7 +1011,9 @@ def test_resample_to_period_monthly_buglet(unit):
rng = date_range("1/1/2000", "12/31/2000").as_unit(unit)
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)

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

Expand Down Expand Up @@ -1126,14 +1132,18 @@ def test_resample_anchored_intraday(simple_date_range_series, unit):
df = DataFrame(rng.month, index=rng)

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

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

exp.index = exp.index + Timedelta(1, "ns") - Timedelta(1, "D")
Expand All @@ -1145,15 +1155,21 @@ def test_resample_anchored_intraday(simple_date_range_series, unit):
df = DataFrame(rng.month, index=rng)

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

result = df.resample("QE", closed="left").mean()
expected = df.shift(1, freq="D").resample("QE", kind="period", closed="left").mean()
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
expected = (
df.shift(1, freq="D").resample("QE", kind="period", closed="left").mean()
)
expected = expected.to_timestamp(how="end")
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
expected.index._data.freq = "QE"
Expand Down Expand Up @@ -1216,7 +1232,9 @@ def test_corner_cases_date(simple_date_range_series, unit):
# resample to periods
ts = simple_date_range_series("2000-04-28", "2000-04-30 11:00", freq="h")
ts.index = ts.index.as_unit(unit)
result = ts.resample("ME", kind="period").mean()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ts.resample("ME", kind="period").mean()
assert len(result) == 1
assert result.index[0] == Period("2000-04", freq="M")

Expand Down
47 changes: 35 additions & 12 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def test_asfreq(self, series_and_frame, freq, kind):
end = (obj.index[-1] + obj.index.freq).to_timestamp(how="start")
new_index = date_range(start=start, end=end, freq=freq, inclusive="left")
expected = obj.to_timestamp().reindex(new_index).to_period(freq)
result = obj.resample(freq, kind=kind).asfreq()
msg = "The 'kind' keyword in (Series|DataFrame).resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = obj.resample(freq, kind=kind).asfreq()
tm.assert_almost_equal(result, expected)

def test_asfreq_fill_value(self, series):
Expand All @@ -68,7 +70,9 @@ def test_asfreq_fill_value(self, series):
freq="1h",
)
expected = s.to_timestamp().reindex(new_index, fill_value=4.0)
result = s.resample("1h", kind="timestamp").asfreq(fill_value=4.0)
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.resample("1h", kind="timestamp").asfreq(fill_value=4.0)
tm.assert_series_equal(result, expected)

frame = s.to_frame("value")
Expand All @@ -78,7 +82,9 @@ def test_asfreq_fill_value(self, series):
freq="1h",
)
expected = frame.to_timestamp().reindex(new_index, fill_value=3.0)
result = frame.resample("1h", kind="timestamp").asfreq(fill_value=3.0)
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = frame.resample("1h", kind="timestamp").asfreq(fill_value=3.0)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("freq", ["h", "12h", "2D", "W"])
Expand All @@ -97,8 +103,10 @@ def test_selection(self, index, freq, kind, kwargs):
r"not currently supported, use \.set_index\(\.\.\.\) to "
"explicitly set index"
)
depr_msg = "The 'kind' keyword in DataFrame.resample is deprecated"
with pytest.raises(NotImplementedError, match=msg):
df.resample(freq, kind=kind, **kwargs)
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
df.resample(freq, kind=kind, **kwargs)

@pytest.mark.parametrize("month", MONTHS)
@pytest.mark.parametrize("meth", ["ffill", "bfill"])
Expand Down Expand Up @@ -227,9 +235,12 @@ def test_resample_basic(self):
name="idx",
)
expected = Series([34.5, 79.5], index=index)
result = s.to_period().resample("min", kind="period").mean()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.to_period().resample("min", kind="period").mean()
tm.assert_series_equal(result, expected)
result2 = s.resample("min", kind="period").mean()
with tm.assert_produces_warning(FutureWarning, match=msg):
result2 = s.resample("min", kind="period").mean()
tm.assert_series_equal(result2, expected)

@pytest.mark.parametrize(
Expand Down Expand Up @@ -275,7 +286,9 @@ def test_with_local_timezone_pytz(self):

series = Series(1, index=index)
series = series.tz_convert(local_timezone)
result = series.resample("D", kind="period").mean()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = series.resample("D", kind="period").mean()

# Create the expected series
# Index is moved back a day with the timezone conversion from UTC to
Expand Down Expand Up @@ -316,7 +329,9 @@ def test_with_local_timezone_dateutil(self):

series = Series(1, index=index)
series = series.tz_convert(local_timezone)
result = series.resample("D", kind="period").mean()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = series.resample("D", kind="period").mean()

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

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

Expand Down Expand Up @@ -450,7 +467,9 @@ def test_resample_5minute(self, freq, kind):
expected = ts.to_timestamp().resample(freq).mean()
if kind != "timestamp":
expected = expected.to_period(freq)
result = ts.resample(freq, kind=kind).mean()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ts.resample(freq, kind=kind).mean()
tm.assert_series_equal(result, expected)

def test_upsample_daily_business_daily(self, simple_period_range_series):
Expand Down Expand Up @@ -523,7 +542,9 @@ def test_resample_tz_localized(self):
tm.assert_series_equal(result, expected)

# for good measure
result = s.resample("D", kind="period").mean()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.resample("D", kind="period").mean()
ex_index = period_range("2001-09-20", periods=1, freq="D")
expected = Series([1.5], index=ex_index)
tm.assert_series_equal(result, expected)
Expand Down Expand Up @@ -764,7 +785,9 @@ def test_upsampling_ohlc(self, freq, period_mult, kind):
# of the last original period, so extend accordingly:
new_index = period_range(start="2000", freq=freq, periods=period_mult * len(pi))
expected = expected.reindex(new_index)
result = s.resample(freq, kind=kind).ohlc()
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.resample(freq, kind=kind).ohlc()
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
Expand Down
Loading