Skip to content
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

Merged

Conversation

natmokval
Copy link
Contributor

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'

@mroeschke mroeschke added Frequency DateOffsets Period Period data type labels Nov 6, 2023
@natmokval natmokval marked this pull request as ready for review November 6, 2023 19:46
Comment on lines 2753 to 2756
if freq.__class__ in PERIOD_DEPR_OFFSETS:
raise ValueError(
f"{(type(freq).__name__)} is not supported as period frequency"
)
Copy link
Member

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(...)

?

Copy link
Contributor Author

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' ...

Copy link
Member

@MarcoGorelli MarcoGorelli Nov 7, 2023

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?

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, I did as you suggested. It works very well.

Copy link
Member

@MarcoGorelli MarcoGorelli left a 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

@@ -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`)
Copy link
Member

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`)

Copy link
Contributor Author

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.

@MarcoGorelli MarcoGorelli added this to the 2.2 milestone Nov 7, 2023
@natmokval natmokval changed the title Raise ValueError for offsets which are not supported as period frequency ENH: Raise ValueError for offsets which are not supported as period frequency Nov 7, 2023
@natmokval natmokval changed the title ENH: Raise ValueError for offsets which are not supported as period frequency ENH: Raise TypeError for offsets which are not supported as period frequency Nov 7, 2023
@mroeschke mroeschke merged commit b8fc8cd into pandas-dev:main Nov 7, 2023
@mroeschke
Copy link
Member

Thanks @natmokval

@cancan101
Copy link
Contributor

@natmokval, does this PR also close: #5091 ?

@natmokval
Copy link
Contributor Author

@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':

>>> ts = pd.date_range('1/1/2012', periods=4, freq=pd.offsets.MonthEnd())
>>> per = ts.to_period()
>>> ts_per = per.to_timestamp()
>>> ts
DatetimeIndex(['2012-01-31', '2012-02-29', '2012-03-31', '2012-04-30'], dtype='datetime64[ns]', freq='ME')
>>> per
PeriodIndex(['2012-01', '2012-02', '2012-03', '2012-04'], dtype='period[M]')
>>> ts_per
DatetimeIndex(['2012-01-01', '2012-02-01', '2012-03-01', '2012-04-01'], dtype='datetime64[ns]', freq='MS')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frequency DateOffsets Period Period data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: QuarterBegin Does not work with Period
4 participants