Skip to content

Why does infer_freq raise an error for PeriodIndex? #6771

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

Open
jseabold opened this issue Apr 2, 2014 · 4 comments
Open

Why does infer_freq raise an error for PeriodIndex? #6771

jseabold opened this issue Apr 2, 2014 · 4 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Frequency DateOffsets Period Period data type

Comments

@jseabold
Copy link
Contributor

jseabold commented Apr 2, 2014

xref #8466

Can't it just return the freq for you instead of raising an error? Helps with duck typing.

[~/]
[1]: pd.version.version
[1]: '0.13.1-434-gdcbbc59'

[2]: pd.tseries.frequencies.infer_freq(pd.PeriodIndex(['2000-01-01'], freq='D'))

Error message

  File "/home/skipper/src/pandas-skipper/pandas/tseries/frequencies.py", line 656, in infer_freq
    raise TypeError("PeriodIndex given. Check the `freq` attribute "
TypeError: PeriodIndex given. Check the `freq` attribute instead of using infer_freq.
@jtratner
Copy link
Contributor

jtratner commented Apr 5, 2014

I agree, seems weird at a surface level. That said, turns out you added this in with 32766d6 and initially raised the issue #5841 for it. Looks like there can be some strangeness with multiple periods in a periodindex or something? I'm not totally clear.

@jseabold
Copy link
Contributor Author

jseabold commented Apr 5, 2014

Ah, well that was before I realized that freq/freqstr doesn't consistently meet all of my needs. Now I'm using infer_freq everywhere. Cf. #6637. Is it just my misunderstandings or does this all seems overly complicated? Convenience vs. "optimized" code. I'm not sure all those milliseconds I have saved are bigger than the time I've spent on these things.

Multiple periods in a PeriodIndex? Doesn't that defeat the whole purpose?

@jreback
Copy link
Contributor

jreback commented Apr 5, 2014

the issue is that if you have a list of periods and you want to construct a Periodindex then in theory you should infer a new freq (eg say u string together 4 month periods that are quarterly)

I think this is pretty ambiguous and that is the issue (while time stamps can be too it is less so)

that said u can simply make sure that all the periods are the same freq and just return it

@jseabold
Copy link
Contributor Author

jseabold commented Apr 5, 2014

I see. I never even thought of this. I always assume that PeriodIndex is an index of consecutive (missing values allowed) Period types with the frequency correctly defined i.e., you never get monthly periods to represent a quarterly index. If we're given a PeriodIndex of monthly Periods that are supposed to be quarterly, then we (statsmodels) are going to incorrectly extrapolate out of sample.

The ambiguity is that it's not clear whether you should return monthly or quarterly? I'd argue to return quarterly. The use case being, I want to extrapolate out of sample. The offset should be (inferred to be) quarterly not monthly.

It makes sense to me not to allow a difference between frequency and offset in PeriodIndex. PeriodIndex should be the simple beast and DatetimeIndex the more general where weirdness is allowed and frequency inference fails no? Maybe people are (ab)using PeriodIndex in that way, but I don't think you should let them. Anarchy!

@jreback jreback added this to the 0.15.0 milestone Apr 6, 2014
@jreback jreback added the Frequency DateOffsets label Oct 5, 2014
@jreback jreback modified the milestones: 0.16, 0.15.1 Oct 5, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@jbrockmendel jbrockmendel added Error Reporting Incorrect or improved errors from pandas and removed Enhancement labels Dec 18, 2019
@mroeschke mroeschke added the Bug label May 11, 2020
@mroeschke mroeschke added Enhancement and removed Bug labels Apr 11, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Frequency DateOffsets Period Period data type
Projects
None yet
Development

No branches or pull requests

5 participants