Skip to content

Period.asfreq broken for freq=MS #5332

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

Closed
cancan101 opened this issue Oct 25, 2013 · 10 comments
Closed

Period.asfreq broken for freq=MS #5332

cancan101 opened this issue Oct 25, 2013 · 10 comments
Labels
Error Reporting Incorrect or improved errors from pandas Frequency DateOffsets Period Period data type
Milestone

Comments

@cancan101
Copy link
Contributor

This works:

In [9]: pd.Period("2013").asfreq(freq="M", how="S")
Out[9]: Period('2013-01', 'M')

but this does not:

In [8]: pd.Period("2013").asfreq(freq="MS", how="S")
----> 1 pd.Period("2013").asfreq(freq="MS", how="S")

/usr/local/lib/python2.7/dist-packages/pandas/tseries/period.pyc in asfreq(self, freq, how)
    196         how = _validate_end_alias(how)
    197         base1, mult1 = _gfc(self.freq)
--> 198         base2, mult2 = _gfc(freq)
    199 
    200         if mult2 != 1:

/usr/local/lib/python2.7/dist-packages/pandas/tseries/frequencies.pyc in get_freq_code(freqstr)
     99 
    100     base, stride = _base_and_stride(freqstr)
--> 101     code = _period_str_to_code(base)
    102 
    103     return code, stride

/usr/local/lib/python2.7/dist-packages/pandas/tseries/frequencies.pyc in _period_str_to_code(freqstr)
    744         return _period_code_map[freqstr]
    745     except:
--> 746         alias = _period_alias_dict[freqstr]
    747         return _period_code_map[alias]
    748 

KeyError: 'L'

it seems to be treating the capital MS as lower case ms, meaning millisecond. Observer similar results:

In [11]: pd.Period("2013").asfreq(freq="ms", how="S")
----> 1 pd.Period("2013").asfreq(freq="ms", how="S")

/usr/local/lib/python2.7/dist-packages/pandas/tseries/period.pyc in asfreq(self, freq, how)
    196         how = _validate_end_alias(how)
    197         base1, mult1 = _gfc(self.freq)
--> 198         base2, mult2 = _gfc(freq)
    199 
    200         if mult2 != 1:

/usr/local/lib/python2.7/dist-packages/pandas/tseries/frequencies.pyc in get_freq_code(freqstr)
     99 
    100     base, stride = _base_and_stride(freqstr)
--> 101     code = _period_str_to_code(base)
    102 
    103     return code, stride

/usr/local/lib/python2.7/dist-packages/pandas/tseries/frequencies.pyc in _period_str_to_code(freqstr)
    744         return _period_code_map[freqstr]
    745     except:
--> 746         alias = _period_alias_dict[freqstr]
    747         return _period_code_map[alias]
    748 

KeyError: 'L'
@jtratner
Copy link
Contributor

You've been looking through, right? Do you know where to find the fix? (I'm pretty sure it's because it lower cases the name when trying to get the alias from the period string).

Also, this was in v0.12.0 as well, so wasn't introduced by recent refactoring.

@cancan101
Copy link
Contributor Author

Agreed that it was not recently added. I did actually test that. I can look into where the lower case call is.

@cancan101
Copy link
Contributor Author

It looks like the get_offset function has explicit code to deal with this.

@cancan101
Copy link
Contributor Author

So this is slightly more complicated:

def _period_alias_dictionary():
   L_aliases = ["L", "MS", "MILLISECOND", "MILLISECONDLY"]

would it be okay to remove MS as an alias to L ? At the very the capital version is ambiguous. Other parts of the code explicitly only look at lowercase ms for milliseconds which i WILL maintain as an alias for L.

@cancan101
Copy link
Contributor Author

Come to think of it, I am not sure that MS make sense as a period frequency. That being said it seems safest to not allow MS to be used to mean milliseconds as that is ambiguous. It's pretty easy to make MS unknown rather than mapped to milliseconds. Thoughts?

@jtratner
Copy link
Contributor

@cancan101 this is very odd - I don't see the error anymore. I ran your code, got the KeyError, but now I'm working on the same master as before and I get this:

In [2]: pd.Period("2013").asfreq(freq="MS", how="S")
Out[2]: Period('2013-01-01 00:00:00.000', 'L')

and this

In [3]: pd.Period("2013").asfreq(freq="MS")
Out[3]: Period('2013-12-31 23:59:59.999', 'L')

@cancan101
Copy link
Contributor Author

That is very odd. I have a simple fix for this BTW.

On Fri, Oct 25, 2013 at 8:54 PM, Jeff Tratner [email protected]:

@cancan101 https://github.com/cancan101 this is very odd - I don't see
the error anymore. I ran your code, got the KeyError, but now I'm working
on the same master as before and I get this:

In [2]: pd.Period("2013").asfreq(freq="MS", how="S")Out[2]: Period('2013-01-01 00:00:00.000', 'L')

and this

In [3]: pd.Period("2013").asfreq(freq="MS")Out[3]: Period('2013-12-31 23:59:59.999', 'L')


Reply to this email directly or view it on GitHubhttps://github.com//issues/5332#issuecomment-27135898
.

@cancan101
Copy link
Contributor Author

cancan101@0173f2d

@cancan101
Copy link
Contributor Author

Let me know if that's OK and I will send PR.

@jreback
Copy link
Contributor

jreback commented Apr 9, 2014

closed via #5340

@jreback jreback closed this as completed Apr 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Frequency DateOffsets Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants