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

TST: assert that informative error is raised when offset not supported as a period frequency is passed to DataFrame.asfreq #56758

Conversation

aram-cinnamon
Copy link
Contributor

@aram-cinnamon aram-cinnamon commented Jan 7, 2024

pandas/core/arrays/period.py Outdated Show resolved Hide resolved
with pytest.raises(
TypeError, match='"2MS" is not supported as a period frequency'
):
df.asfreq(freq="2MS")
Copy link
Member

Choose a reason for hiding this comment

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

Can you parametrize over a BaseOffset and the freq given as a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, although this PR isn't really about the offset cases

),
(
offsets.MonthBegin(),
ValueError,
Copy link
Member

Choose a reason for hiding this comment

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

It looks like those are caught before they reach your new error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noting that Patrick and I chatted over Slack. Summary: The change in this PR raises an error for invalid strings. Another part of the code raises errors for for invalid offsets (That was already implemented, before this PR).

@aram-cinnamon aram-cinnamon changed the title raise TypeError if an offset not supported as a period frequency is passed to asfreq raise TypeError if an offset (str) not supported as a period frequency is passed to asfreq Jan 9, 2024
@aram-cinnamon aram-cinnamon changed the title raise TypeError if an offset (str) not supported as a period frequency is passed to asfreq ENH: raise TypeError if an offset (str) not supported as a period frequency is passed to asfreq Jan 19, 2024
@aram-cinnamon
Copy link
Contributor Author

@phofl anything I should do now to move this forward?

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.

thanks for your PR - to be honest I think this validation needs doing a bit deeper, in to_offset - then a lot of other parts of the codebase could benefit from it

I think @natmokval is working on that - is it OK if we put this on pause until we've seen whether that works?

Sorry that you already spent time on this

Comment on lines 740 to 742
freq = Period._maybe_convert_freq(freq)
if not hasattr(freq, "_period_dtype_code"):
raise TypeError(f'"{freq_original}" is not supported as a 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.

in #56945 (review) I'm suggesting to raise if the return value of to_offset(..., is_period=True) doesn't have a _period_dtype_code attribute

If that works, then it would resolve the linked issue too because Period._maybe_convert_freq would go down that path

@MarcoGorelli
Copy link
Member

this has been addressed by #56945

really sorry that you spent time on this one, and I hope you got something out of the experience anyway - i hate to do this, but i'll close then

@MarcoGorelli
Copy link
Member

MarcoGorelli commented Feb 8, 2024

reopening, as:

  • the tests from this PR are different to those in the linked one, so they would still be good to add
  • I just didn't handle this well, I wasn't aware of this PR when I'd started reviewing the other one

will add a commit soon-ish (early next week) to just keep the tests, then we can merge it in

I didn't get this right as reviewer, I need to own up to this

@MarcoGorelli MarcoGorelli reopened this Feb 8, 2024
@MarcoGorelli MarcoGorelli changed the title ENH: raise TypeError if an offset (str) not supported as a period frequency is passed to asfreq TST: assert that informative error is raised when offset not supported as a period frequency is passed to DataFrame.asfreq Feb 12, 2024
@MarcoGorelli MarcoGorelli added the Needs Tests Unit test(s) needed to prevent regressions label Feb 12, 2024
@MarcoGorelli MarcoGorelli added this to the 3.0 milestone Feb 12, 2024
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.

thanks @aram-cinnamon !

@MarcoGorelli MarcoGorelli merged commit dfb3f6c into pandas-dev:main Feb 12, 2024
50 of 51 checks passed
pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
…d as a period frequency is passed to DataFrame.asfreq (pandas-dev#56758)

* style

* add test

* pytest.raises

* freqstr

* add test for offsets

* rearrange per comment

* fixup

* fixup

---------

Co-authored-by: MarcoGorelli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: raise TypeError if offsets which are not supported as period frequency pass to asfreq
3 participants