-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
Changes from 9 commits
df59899
c970eba
275524e
e853b4a
65627ec
06592df
61f50e5
9bdd90f
c50848e
2b507af
0fff4fd
6e511ad
35ae66f
8ca1597
9c0c230
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think you can remove this test entirely There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, done |
||
df.resample(freq).mean() | ||
|
||
|
||
def test_resample_upsample(unit): | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
||
|
@@ -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") | ||
|
@@ -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" | ||
|
@@ -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") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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"]) | ||
|
@@ -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( | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -489,16 +479,17 @@ def test_cant_fill_missing_dups(self): | |
s.resample("Y").ffill() | ||
|
||
@pytest.mark.parametrize("freq", ["5min"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you inline this parameter since it's the only one? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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( | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
fromget_resampler
There was a problem hiding this comment.
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 inresample.py
be removable?There was a problem hiding this comment.
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 fromResampler
andTimeGrouper
. I have updated this PR. Could you please take a look?