Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2827,6 +2827,16 @@ def asfreq(
if how is None:
how = "E"

if isinstance(freq, str):
Copy link
Member

Choose a reason for hiding this comment

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

a few lines down, there's obj.index.asfreq(freq, how=how) - should these validation go in there?

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, you are right. I moved the check if isinstance(freq, str) to the definition of asfreq in pandas/core/indexes/period.py

freq = to_offset(freq, is_period=True)
if hasattr(freq, "_period_dtype_code"):
freq = freq_to_period_freqstr(freq.n, freq.name)
else:
raise ValueError(
f"Invalid offset: '{freq.name}' for converting time series "
f"with PeriodIndex."
)

if isinstance(freq, BaseOffset):
if hasattr(freq, "_period_dtype_code"):
freq = freq_to_period_freqstr(freq.n, freq.name)
Expand Down
20 changes: 18 additions & 2 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,14 +1032,30 @@ def test_resample_lowercase_frequency_deprecated(
offsets.BusinessHour(2),
],
)
def test_asfreq_invalid_period_freq(self, offset, frame_or_series):
# GH#9586
def test_asfreq_invalid_period_offset(self, offset, frame_or_series):
# GH#55785
msg = f"Invalid offset: '{offset.base}' for converting time series "

obj = frame_or_series(range(5), index=period_range("2020-01-01", periods=5))
with pytest.raises(ValueError, match=msg):
obj.asfreq(freq=offset)

@pytest.mark.parametrize(
"freq",
[
"2BMS",
"2YS-MAR",
"2bh",
],
)
def test_asfreq_invalid_period_freq(self, freq, frame_or_series):
# GH#55785
msg = f"Invalid offset: '{freq[1:]}' for converting time series "

obj = frame_or_series(range(5), index=period_range("2020-01-01", periods=5))
with pytest.raises(ValueError, match=msg):
obj.asfreq(freq=freq)


@pytest.mark.parametrize(
"freq,freq_depr",
Expand Down