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

CLN: enforce deprecation of the kind keyword on df.resample/ser.resample #58125

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ Removal of prior version deprecations/changes
- Disallow units other than "s", "ms", "us", "ns" for datetime64 and timedelta64 dtypes in :func:`array` (:issue:`53817`)
- Removed "freq" keyword from :class:`PeriodArray` constructor, use "dtype" instead (:issue:`52462`)
- Removed 'fastpath' keyword in :class:`Categorical` constructor (:issue:`20110`)
- Removed 'kind' keyword in :meth:`Series.resample` and :meth:`DataFrame.resample` (:issue:`58125`)
- Removed alias :class:`arrays.PandasArray` for :class:`arrays.NumpyExtensionArray` (:issue:`53694`)
- Removed deprecated "method" and "limit" keywords from :meth:`Series.replace` and :meth:`DataFrame.replace` (:issue:`53492`)
- Removed extension test classes ``BaseNoReduceTests``, ``BaseNumericReduceTests``, ``BaseBooleanReduceTests`` (:issue:`54663`)
Expand Down
22 changes: 0 additions & 22 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8615,7 +8615,6 @@ def resample(
closed: Literal["right", "left"] | None = None,
label: Literal["right", "left"] | None = None,
convention: Literal["start", "end", "s", "e"] | lib.NoDefault = lib.no_default,
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 @@ -8648,14 +8647,6 @@ def resample(

.. deprecated:: 2.2.0
Convert PeriodIndex to DatetimeIndex before resampling instead.
kind : {{'timestamp', 'period'}}, optional, default None
Pass 'timestamp' to convert the resulting index to a
`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 @@ -8947,18 +8938,6 @@ def resample(
"""
from pandas.core.resample import get_resampler

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

if convention is not lib.no_default:
warnings.warn(
f"The 'convention' keyword in {type(self).__name__}.resample is "
Expand All @@ -8976,7 +8955,6 @@ def resample(
freq=rule,
label=label,
closed=closed,
kind=kind,
Copy link
Member

Choose a reason for hiding this comment

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

i dont see a diff in pandas.core.resample; doesn't the keyword need to be removed there too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, my mistake. I removed the keyword kind from get_resampler

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't all/most of the kind keywords in resample.py be removable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment. Yes, it was my mistake; kind can be removed from Resampler and TimeGrouper. I have updated this PR. Could you please take a look?

convention=convention,
key=on,
level=level,
Expand Down
37 changes: 11 additions & 26 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,7 @@ def test_resample_frame_basic_kind(freq, unit):
index=date_range("2000-01-01", periods=10, freq="B"),
)
df.index = df.index.as_unit(unit)
msg = "The 'kind' keyword in DataFrame.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df.resample(freq, kind="period").mean()
Copy link
Member

Choose a reason for hiding this comment

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

i think you can remove this test entirely

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, done

df.resample(freq).mean()


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

msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ts.resample(freq, kind="period").mean()
result = ts.resample(freq).mean().to_period()
expected = ts.resample(freq).mean()
expected.index = period_range(**expected_kwargs)
tm.assert_series_equal(result, expected)
Expand Down Expand Up @@ -985,9 +981,7 @@ 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)

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

Expand Down Expand Up @@ -1109,18 +1103,15 @@ def test_resample_anchored_intraday(unit):
df = DataFrame(rng.month, index=rng)

result = df.resample("ME").mean()
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 = df.resample("ME").mean().to_period()
expected = expected.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()
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 = df.shift(1, freq="D").resample("ME").mean().to_period()
exp = exp.to_timestamp(how="end")

exp.index = exp.index + Timedelta(1, "ns") - Timedelta(1, "D")
Expand All @@ -1134,21 +1125,17 @@ def test_resample_anchored_intraday2(unit):
df = DataFrame(rng.month, index=rng)

result = df.resample("QE").mean()
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 = df.resample("QE").mean().to_period()
expected = expected.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()
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 = df.shift(1, freq="D").resample("QE").mean()
expected = expected.to_period()
expected = expected.to_timestamp(how="end")
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
expected.index._data.freq = "QE"
Expand Down Expand Up @@ -1205,9 +1192,7 @@ 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)
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ts.resample("ME", kind="period").mean()
result = ts.resample("ME").mean().to_period()
assert len(result) == 1
assert result.index[0] == Period("2000-04", freq="M")

Expand Down
93 changes: 42 additions & 51 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,24 @@ def _simple_period_range_series(start, end, freq="D"):

class TestPeriodIndex:
@pytest.mark.parametrize("freq", ["2D", "1h", "2h"])
@pytest.mark.parametrize("kind", ["period", None, "timestamp"])
def test_asfreq(self, frame_or_series, freq, kind):
def test_asfreq(self, frame_or_series, freq):
# GH 12884, 15944
# make sure .asfreq() returns PeriodIndex (except kind='timestamp')

obj = frame_or_series(range(5), index=period_range("2020-01-01", periods=5))
if kind == "timestamp":
expected = obj.to_timestamp().resample(freq).asfreq()
else:
start = obj.index[0].to_timestamp(how="start")
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)
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()

expected = obj.to_timestamp().resample(freq).asfreq()
result = obj.to_timestamp().resample(freq).asfreq()
tm.assert_almost_equal(result, expected)

start = obj.index[0].to_timestamp(how="start")
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).asfreq()
tm.assert_almost_equal(result, expected)

result = obj.resample(freq).asfreq().to_timestamp().to_period()
tm.assert_almost_equal(result, expected)

def test_asfreq_fill_value(self):
Expand All @@ -88,9 +90,7 @@ def test_asfreq_fill_value(self):
freq="1h",
)
expected = s.to_timestamp().reindex(new_index, 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)
result = s.to_timestamp().resample("1h").asfreq(fill_value=4.0)
tm.assert_series_equal(result, expected)

frame = s.to_frame("value")
Expand All @@ -100,15 +100,12 @@ def test_asfreq_fill_value(self):
freq="1h",
)
expected = frame.to_timestamp().reindex(new_index, 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)
result = frame.to_timestamp().resample("1h").asfreq(fill_value=3.0)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("freq", ["h", "12h", "2D", "W"])
@pytest.mark.parametrize("kind", [None, "period", "timestamp"])
@pytest.mark.parametrize("kwargs", [{"on": "date"}, {"level": "d"}])
def test_selection(self, freq, kind, kwargs):
def test_selection(self, freq, kwargs):
# This is a bug, these should be implemented
# GH 14008
index = period_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
Expand All @@ -122,10 +119,8 @@ def test_selection(self, 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):
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
df.resample(freq, kind=kind, **kwargs)
df.resample(freq, **kwargs)

@pytest.mark.parametrize("month", MONTHS)
@pytest.mark.parametrize("meth", ["ffill", "bfill"])
Expand Down Expand Up @@ -268,12 +263,9 @@ def test_resample_basic(self):
name="idx",
)
expected = Series([34.5, 79.5], index=index)
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()
result = s.to_period().resample("min").mean()
tm.assert_series_equal(result, expected)
with tm.assert_produces_warning(FutureWarning, match=msg):
result2 = s.resample("min", kind="period").mean()
result2 = s.resample("min").mean().to_period()
tm.assert_series_equal(result2, expected)

@pytest.mark.parametrize(
Expand Down Expand Up @@ -328,9 +320,9 @@ def test_with_local_timezone(self, tz):

series = Series(1, index=index)
series = series.tz_convert(local_timezone)
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = series.resample("D", kind="period").mean()
msg = "Converting to PeriodArray/Index representation will drop timezone"
with tm.assert_produces_warning(UserWarning, match=msg):
result = series.resample("D").mean().to_period()

# Create the expected series
# Index is moved back a day with the timezone conversion from UTC to
Expand Down Expand Up @@ -432,10 +424,8 @@ 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")

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()
result = ts.resample("Y-DEC").mean().to_timestamp()
expected = ts.resample("Y-DEC").mean().to_timestamp(how="start")
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("month", MONTHS)
Expand Down Expand Up @@ -489,16 +479,17 @@ def test_cant_fill_missing_dups(self):
s.resample("Y").ffill()

@pytest.mark.parametrize("freq", ["5min"])
Copy link
Member

Choose a reason for hiding this comment

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

Could you inline this parameter since it's the only one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@pytest.mark.parametrize("kind", ["period", None, "timestamp"])
def test_resample_5minute(self, freq, kind):
def test_resample_5minute(self, freq):
rng = period_range("1/1/2000", "1/5/2000", freq="min")
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
expected = ts.to_timestamp().resample(freq).mean()
if kind != "timestamp":
expected = expected.to_period(freq)
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ts.resample(freq, kind=kind).mean()
result = ts.resample(freq).mean().to_timestamp()
tm.assert_series_equal(result, expected)

expected = expected.to_period(freq)
result = ts.resample(freq).mean()
tm.assert_series_equal(result, expected)
result = ts.resample(freq).mean().to_timestamp().to_period()
tm.assert_series_equal(result, expected)

def test_upsample_daily_business_daily(self, simple_period_range_series):
Expand Down Expand Up @@ -572,9 +563,9 @@ def test_resample_tz_localized2(self):
tm.assert_series_equal(result, expected)

# for good measure
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.resample("D", kind="period").mean()
msg = "Converting to PeriodArray/Index representation will drop timezone "
with tm.assert_produces_warning(UserWarning, match=msg):
result = s.resample("D").mean().to_period()
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 @@ -808,8 +799,7 @@ def test_evenly_divisible_with_no_extra_bins2(self):
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("freq, period_mult", [("h", 24), ("12h", 2)])
@pytest.mark.parametrize("kind", [None, "period"])
def test_upsampling_ohlc(self, freq, period_mult, kind):
def test_upsampling_ohlc(self, freq, period_mult):
# GH 13083
pi = period_range(start="2000", freq="D", periods=10)
s = Series(range(len(pi)), index=pi)
Expand All @@ -819,9 +809,10 @@ 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)
msg = "The 'kind' keyword in Series.resample is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.resample(freq, kind=kind).ohlc()
result = s.resample(freq).ohlc()
tm.assert_frame_equal(result, expected)

result = s.resample(freq).ohlc().to_timestamp().to_period()
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
Expand Down