Skip to content

ENH: Period to accept datetime64 value? #9054

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
jorisvandenbossche opened this issue Dec 11, 2014 · 1 comment · Fixed by #9896
Closed

ENH: Period to accept datetime64 value? #9054

jorisvandenbossche opened this issue Dec 11, 2014 · 1 comment · Fixed by #9896
Labels
Dtype Conversions Unexpected or buggy dtype conversions Enhancement Period Period data type
Milestone

Comments

@jorisvandenbossche
Copy link
Member

I didn't directly find something about this, but is there a reason why the Period(Index) constructor does not take datetime64 values? As it does accept datetime:

In [15]: t = pd.Timestamp('2012-01-01')

In [16]: t.to_pydatetime()
Out[16]: datetime.datetime(2012, 1, 1, 0, 0)

In [17]: t.asm8
Out[17]: numpy.datetime64('2012-01-01T01:00:00.000000000+0100')

In [19]: pd.Period(t, freq='D')
Out[19]: Period('2012-01-01', 'D')

In [20]: pd.Period(t.to_pydatetime(), freq='D')
Out[20]: Period('2012-01-01', 'D')

In [21]: pd.Period(t.asm8, freq='D')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-f08079765cf6> in <module>()
----> 1 pd.Period(t.asm8, freq='D')

C:\Anaconda\lib\site-packages\pandas\tseries\period.pyc in __init__(self, value,
 freq, ordinal, year, month, quarter, day, hour, minute, second)
    141         else:
    142             msg = "Value must be Period, string, integer, or datetime"
--> 143             raise ValueError(msg)
    144
    145         base, mult = _gfc(freq)

ValueError: Value must be Period, string, integer, or datetime
@jreback
Copy link
Contributor

jreback commented Dec 11, 2014

no I agree it should

https://github.com/pydata/pandas/blob/master/pandas/tseries/period.py#L133
should accept a np.datetime64
then wrap in a Timestamp before the next step (as it needs to be a datetime)

@jreback jreback added Period Period data type Dtype Conversions Unexpected or buggy dtype conversions Good as first PR labels Dec 11, 2014
@jreback jreback added this to the 0.16.0 milestone Dec 11, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
jcrist added a commit to jcrist/pandas that referenced this issue Apr 13, 2015
Added support for `datetime64` value for Period. Fixes pandas-dev#9054.
@jreback jreback modified the milestones: 0.16.1, Next Major Release Apr 13, 2015
jcrist added a commit to jcrist/pandas that referenced this issue Apr 13, 2015
Added support for `datetime64` value for Period. Fixes pandas-dev#9054.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Enhancement Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants