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

CLN: replace unnecessary freq_to_period_freqstr with PeriodDtype constructor #56550

Merged
merged 18 commits into from
Feb 28, 2024

Conversation

natmokval
Copy link
Contributor

xref #52064

replaced freq_to_period_freqstr() with PeriodDtype(freq)._freqstr in ValueError message in the definition of _shift_with_freq()

@natmokval natmokval marked this pull request as ready for review December 18, 2023 14:05
@natmokval natmokval added the Frequency DateOffsets label Dec 18, 2023
f"Given freq {freq_to_period_freqstr(freq.n, freq.name)} "
f"Given freq {PeriodDtype(freq)._freqstr} "
Copy link
Member

Choose a reason for hiding this comment

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

does this change the message? if so, could you show before vs after please?

if not, then is it possible to do this replacement across the whole codebase and get rid of freq_to_period_freqstr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

does this change the message? if so, could you show before vs after please?

It doesn't change the message.

if not, then is it possible to do this replacement across the whole codebase and get rid of freq_to_period_freqstr?

I replaced freq_to_period_freqstr with PeriodDtype(offset)._freqstr where it works. Now I am trying to do this replacement in pandas/tseries/frequencies.py and in pandas/_libs/tslibs/period.pyx, but I get failures. I am not sure we can complitely rid of freq_to_period_freqstr

@MarcoGorelli MarcoGorelli changed the title ENH: Improve ValueError message in _shift_with_freq() CLN: replace unnecessary freq_to_period_freqstr with PeriodDtype constructor Jan 11, 2024
@natmokval
Copy link
Contributor Author

natmokval commented Feb 14, 2024

I replaced freq_to_period_freqstr with PeriodDtype(obj)._freqstr / PeriodDtypeBase(obj._period_dtype_code, obj.n)._freqstr. Now we don’t need the function freq_to_period_freqstr, which is why I removed it.

One comment:
In pandas/core/arrays/period.py in the definition of raise_on_incompatible I added

with warnings.catch_warnings():
    warnings.simplefilter("ignore")

because otherwise the test_maybe_convert_timedelta (in pandas/tests/indexes/period/test_period.py) fail.

@MarcoGorelli, could you please take a look at this PR?

Comment on lines 988 to 989
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Copy link
Member

Choose a reason for hiding this comment

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

looks a bit dangerous :) is this for the business day warning? if so, I'd suggest either enforcing that one first, or using warnings.filterwarnings and only filtering the warning you mean to

Copy link
Contributor Author

@natmokval natmokval Feb 15, 2024

Choose a reason for hiding this comment

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

yes, this is for the business day warning. I agree, it's better to use warnings.filterwarnings instead of warnings.simplefilter. I made changes in order to filter: "Future warning "PeriodDtype[B] is deprecated"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can rid of this warnings.filterwarnings after enforcing depreciation of PeriodDtype[B], or we can postpone merging this PR until enforcing deprecation of PeriodDtype[B] is done.

Comment on lines -1725 to +1727
freqstr = freq_to_period_freqstr(self.freq.n, self.freq.name)
other_freqstr = freq_to_period_freqstr(other.n, other.name)
freqstr = PeriodDtypeBase(
self.freq._period_dtype_code, self.freq.n
)._freqstr
if hasattr(other, "_period_dtype_code"):
other_freqstr = PeriodDtypeBase(
other._period_dtype_code, other.n
)._freqstr
else:
other_freqstr = other.freqstr
Copy link
Member

Choose a reason for hiding this comment

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

why do we need the if-else branch for other but not for self.freq?

Copy link
Contributor Author

@natmokval natmokval Feb 15, 2024

Choose a reason for hiding this comment

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

self.freq always has attribute "_period_dtype_code", bur other sometimes doesn't.
For example in many tests in pandas/tests/arithmetic/test_period.py( e.g. test_pi_add_sub_timedeltalike_freq_mismatch_monthly) other is equal pd.offsets.YearBegin(2)

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 one - should be close! just got one more question

Comment on lines 256 to 259
freq = freq_to_period_freqstr(1, df.index.freq.rule_code)
freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1]
Copy link
Member

Choose a reason for hiding this comment

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

why does this one require regex splitting, but others don't?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, seems like we don't need this regex splitting here and can replace freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1] with freq = PeriodDtype(df.index.freq)._freqstr

I will make this change.

There is another test test_line_plot_period_mlt_frame in which we use the same regex splitting. I tried to do the similar replacement (with freq = PeriodDtype(df.index.freq)._freqstr[1:]), but the test failed for "1s".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think in test_line_plot_period_mlt_frame we can change freq from

    freq = re.split(r"\d", PeriodDtype(df.index.freq)._freqstr)[-1]
    freq = df.index.asfreq(freq).freq

to

freq = df.index.freq.rule_code

and then get rid of regex splitting. I modified the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we use the same replacement in test_line_plot_datetime_frame, e.g. replace

freq = PeriodDtype(df.index.freq)._freqstr
freq = df.index.to_period(freq).freq

with

freq = df.index.freq.rule_code

the test passes. Should we modify this test as 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!

@jbrockmendel I think you'd suggested this clean up, fancy taking a quick look?

@MarcoGorelli MarcoGorelli added this to the 3.0 milestone Feb 22, 2024
@mroeschke mroeschke merged commit 69f03a3 into pandas-dev:main Feb 28, 2024
51 checks passed
@mroeschke
Copy link
Member

Thanks @natmokval

pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
…constructor (pandas-dev#56550)

* replace freq_to_period_freqstr() with PeriodDtype(freq)._freqstr in _shift_with_freq()

* replace freq_to_period_freqstr with PeriodDtype()._freqstr except in frequencies.py, period.pyx

* fix pre-commit errors

* remove freq_to_period_freqstr from _maybe_coerce_freq and from tests

* remove freq_to_period_freqstr from period.pyx

* correct raise_on_incompatible

* correct raise_on_incompatible again

* replace warnings.simplefilter with warnings.filterwarnings in raise_on_incompatible

* remove regex splitting from test_line_plot_datetime_frame

* remove regex splitting from test_line_plot_period_mlt_frame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants