-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
ENH: raise ValueError if invalid period freq pass to asfreq when the index of df is a PeriodIndex #56945
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
ENH: raise ValueError if invalid period freq pass to asfreq when the index of df is a PeriodIndex #56945
Changes from 3 commits
39f59c4 2b602bd ff1c6e7 fff7c9d a389d11 d2ea914 3735e23 b1e6ba6 c41557a 17fd84c 721637b deea30d 4cbcc5e 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 |
|---|---|---|
| | @@ -16,8 +16,13 @@ | |
| Period, | ||
| Resolution, | ||
| Tick, | ||
| to_offset, | ||
| ) | ||
| from pandas._libs.tslibs.dtypes import OFFSET_TO_PERIOD_FREQSTR | ||
| from pandas._libs.tslibs.dtypes import ( | ||
| OFFSET_TO_PERIOD_FREQSTR, | ||
| freq_to_period_freqstr, | ||
| ) | ||
| from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG | ||
| from pandas.util._decorators import ( | ||
| cache_readonly, | ||
| doc, | ||
| | @@ -205,6 +210,18 @@ def _resolution_obj(self) -> Resolution: | |
| **_shared_doc_kwargs, | ||
| ) | ||
| def asfreq(self, freq=None, how: str = "E") -> Self: | ||
| if isinstance(freq, str): | ||
| offset = to_offset(freq, is_period=True) | ||
| if hasattr(offset, "_period_dtype_code"): | ||
| freq = freq_to_period_freqstr(offset.n, offset.name) | ||
| elif offset.name == freq.replace(f"{offset.n}", ""): | ||
| raise ValueError( | ||
| f"Invalid offset: '{offset.name}' for converting time series " | ||
| f"with PeriodIndex." | ||
| ) | ||
| else: | ||
| raise ValueError(INVALID_FREQ_ERR_MSG.format(f"{freq}")) | ||
| | ||
| arr = self._data.asfreq(freq, how) | ||
| Member 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. can you follow this EDIT: as mentioned in the review, it might be better to go all the way down and do this validation within Contributor Author 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, I did as you suggested and moved this validation to | ||
| return type(self)._simple_new(arr, name=self.name) | ||
| | ||
| | ||
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 don't really understand this, but I think you don't need it - just raise
ValueError(INVALID_FREQ_ERR_MSG.format(f"{freq}"))in theelsebranch below?Uh oh!
There was an error while loading. Please reload this page.
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 use two different error messages here because we raise an error of invalid freq such as
“ABC”and also forfreq=“BMS”which is valid for offsets, but invalid for period. I agree, that it can be confusing, so better to use the standard error messagef"Invalid frequency: {freq}"