Skip to content

ENH: Warn on unused arguments to resample #62101

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

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,22 @@ def __init__(
else:
freq = to_offset(freq)

if not isinstance(freq, Tick):
if offset is not None:
warnings.warn(
"The 'offset' keyword does not take effect when resampling "
"with a 'freq' that is not Tick-like (h, m, s, ms, us, ns)",
RuntimeWarning,
stacklevel=find_stack_level(),
)
if origin != "start_day":
warnings.warn(
"The 'origin' keyword does not take effect when resampling "
"with a 'freq' that is not Tick-like (h, m, s, ms, us, ns)",
RuntimeWarning,
stacklevel=find_stack_level(),
)

end_types = {"ME", "YE", "QE", "BME", "BYE", "BQE", "W"}
rule = freq.rule_code
if rule in end_types or ("-" in rule and rule[: rule.find("-")] in end_types):
Expand Down
28 changes: 17 additions & 11 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,14 +884,14 @@ def test_resample_origin_epoch_with_tz_day_vs_24h(unit):
random_values = np.random.default_rng(2).standard_normal(len(rng))
ts_1 = Series(random_values, index=rng)

result_1 = ts_1.resample("D", origin="epoch").mean()
result_1 = ts_1.resample("D").mean()
result_2 = ts_1.resample("24h", origin="epoch").mean()
tm.assert_series_equal(result_1, result_2, check_freq=False)
# GH#41943 check_freq=False bc Day and Hour(24) no longer compare as equal

# check that we have the same behavior with epoch even if we are not timezone aware
ts_no_tz = ts_1.tz_localize(None)
result_3 = ts_no_tz.resample("D", origin="epoch").mean()
result_3 = ts_no_tz.resample("D").mean()
result_4 = ts_no_tz.resample("24h", origin="epoch").mean()
tm.assert_series_equal(result_1, result_3.tz_localize(rng.tz), check_freq=False)
tm.assert_series_equal(result_1, result_4.tz_localize(rng.tz), check_freq=False)
Expand All @@ -900,7 +900,7 @@ def test_resample_origin_epoch_with_tz_day_vs_24h(unit):
start, end = "2000-10-01 23:30:00+0200", "2000-12-02 00:30:00+0200"
rng = date_range(start, end, freq="7min").as_unit(unit)
ts_2 = Series(random_values, index=rng)
result_5 = ts_2.resample("D", origin="epoch").mean()
result_5 = ts_2.resample("D").mean()
result_6 = ts_2.resample("24h", origin="epoch").mean()
tm.assert_series_equal(result_1.tz_localize(None), result_5.tz_localize(None))
tm.assert_series_equal(result_1.tz_localize(None), result_6.tz_localize(None))
Expand All @@ -909,6 +909,7 @@ def test_resample_origin_epoch_with_tz_day_vs_24h(unit):
def test_resample_origin_with_day_freq_on_dst(unit):
# GH 31809
tz = "America/Chicago"
msg = "The '(origin|offset)' keyword does not take effect"

def _create_series(values, timestamps, freq="D"):
return Series(
Expand All @@ -926,7 +927,9 @@ def _create_series(values, timestamps, freq="D"):

expected = _create_series([24.0, 25.0], ["2013-11-02", "2013-11-03"])
for origin in ["epoch", "start", "start_day", start, None]:
result = ts.resample("D", origin=origin).sum()
warn = RuntimeWarning if origin != "start_day" else None
with tm.assert_produces_warning(warn, match=msg):
result = ts.resample("D", origin=origin).sum()
tm.assert_series_equal(result, expected)

# test complex behavior of origin/offset in a DST context
Expand All @@ -938,7 +941,8 @@ def _create_series(values, timestamps, freq="D"):
# GH#61985 changed this to behave like "B" rather than "24h"
expected_ts = ["2013-11-03 00:00-05:00"]
expected = _create_series([25.0], expected_ts)
result = ts.resample("D", origin="start", offset="-2h").sum()
with tm.assert_produces_warning(RuntimeWarning, match=msg):
result = ts.resample("D", origin="start", offset="-2h").sum()
tm.assert_series_equal(result, expected)

expected_ts = ["2013-11-02 22:00-05:00", "2013-11-03 21:00-06:00"]
Expand All @@ -949,17 +953,20 @@ def _create_series(values, timestamps, freq="D"):
# GH#61985 changed this to behave like "B" rather than "24h"
expected_ts = ["2013-11-03 00:00-05:00"]
expected = _create_series([25.0], expected_ts)
result = ts.resample("D", origin="start", offset="2h").sum()
with tm.assert_produces_warning(RuntimeWarning, match=msg):
result = ts.resample("D", origin="start", offset="2h").sum()
tm.assert_series_equal(result, expected)

expected_ts = ["2013-11-03 00:00-05:00"]
expected = _create_series([25.0], expected_ts)
result = ts.resample("D", origin="start", offset="-1h").sum()
with tm.assert_produces_warning(RuntimeWarning, match=msg):
result = ts.resample("D", origin="start", offset="-1h").sum()
tm.assert_series_equal(result, expected)

expected_ts = ["2013-11-03 00:00-05:00"]
expected = _create_series([25.0], expected_ts)
result = ts.resample("D", origin="start", offset="1h").sum()
with tm.assert_produces_warning(RuntimeWarning, match=msg):
result = ts.resample("D", origin="start", offset="1h").sum()
tm.assert_series_equal(result, expected)


Expand Down Expand Up @@ -2021,9 +2028,8 @@ def test_resample_empty_series_with_tz():
df = DataFrame({"ts": [], "values": []}).astype(
{"ts": "datetime64[ns, Atlantic/Faroe]"}
)
result = df.resample("2MS", on="ts", closed="left", label="left", origin="start")[
"values"
].sum()
rs = df.resample("2MS", on="ts", closed="left", label="left")
result = rs["values"].sum()

expected_idx = DatetimeIndex(
[], freq="2MS", name="ts", dtype="datetime64[ns, Atlantic/Faroe]"
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,10 +921,10 @@ def test_resample_with_offset_month(self):
ser = Series(np.arange(len(pi)), index=pi)
msg = "Resampling with a PeriodIndex is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
rs = ser.resample("M", offset="3h")
rs = ser.resample("M")
result = rs.mean()
result = result.to_timestamp("M")
expected = ser.to_timestamp().resample("ME", offset="3h").mean()
expected = ser.to_timestamp().resample("ME").mean()
# TODO: is non-tick the relevant characteristic? (GH 33815)
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)
Expand Down
Loading