-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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 TypeError for offsets which are not supported as period frequency #55844
ENH: Raise TypeError for offsets which are not supported as period frequency #55844
Conversation
pandas/_libs/tslibs/period.pyx
Outdated
if freq.__class__ in PERIOD_DEPR_OFFSETS: | ||
raise ValueError( | ||
f"{(type(freq).__name__)} is not supported as period frequency" | ||
) |
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.
instead of hard-coding the offsets and checking this, could we just do
if not hasattr(freq, '_period_dtype_code'):
raise ValueError(...)
?
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.
Thank you, I corrected the condition and the error message. We have _period_dtype_code
in class CustomBusinessDay
(see link) that is why I excluded CustomBusinessDay.
I thought, isn't better to use TypeError
instead of ValueError
?
Should we add a note to v2.2.0? Something like Bug in incorrectly allowing construction of :class:'Period' ...
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 - yeah TypeError
is probably better
and yes, a 2.2.0 whatsnew note would be good
maybe we can just do
try:
period_dtype_code = freq._period_dtype_code
except (AttributeError, TypeError):
# AttributeError: _period_dtype_code might not exist
# TypeError: _period_dtype_code might intentionally raise
raise TypeError(f"{(type(freq).__name__)} is not supported as period frequency"
and then reuse period_dtype_code
further down where there's freq._period_dtype_code
?
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, I did as you suggested. It works very well.
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.
Nice work! minor comment on the whatsnew note but the rest looks good on green
doc/source/whatsnew/v2.2.0.rst
Outdated
@@ -432,6 +432,7 @@ I/O | |||
Period | |||
^^^^^^ | |||
- Bug in :class:`Period` addition silently wrapping around instead of raising ``OverflowError`` (:issue:`55503`) | |||
- Bug in incorrectly allowing construction of :class:`Period` with certain offsets which don't have an attribute _period_dtype_code (:issue:`55785`) |
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 think we're giving a better error message here, rather than fixing a bug - could we rephrase to reflect that, and put this in "other enhancements"?
something like
- Improved error message when constructing :class:`Period` with invalid offsets such as "QS" (:issue:`55785`)
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.
agree, I did as you suggested.
Thanks @natmokval |
@natmokval, does this PR also close: #5091 ? |
@cancan101, I think, this PR doesn't close #5091. We still have the same behavior. For example, frequency changes from 'ME' to 'MS':
|
Added to constructor of
Period
check if frequency is supported as period frequency, if not a ValueError is raised.The reason: so far we get
AttributeError: 'pandas._libs.tslibs.offsets.QuarterBegin' object has no attribute '_period_dtype_code'