Skip to content

Series astype() doesn't convert int64 to datetime64 using M8[D] anymore #3969

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
miketkelly opened this issue Jun 20, 2013 · 5 comments
Closed
Labels
Datetime Datetime data dtype Enhancement
Milestone

Comments

@miketkelly
Copy link

add units='D' to Timestamp/to_datetime epoch support

I have an int64 series representing days since epoch which I'd like to convert to datetime64.
This works in 11.0 but raises an exception in 0.11.1.dev-964516a. The exception message is "TypeError: cannot convert datetimelike to dtype [datetime64[D]]".

import pandas as pd
s = pd.Series([0,1,2])
print s.astype('M8[D]')

My workaround is:

import pandas as pd
s = pd.Series([0,1,2])
print pd.Series(s.values.astype('M8[D]'))
@jreback
Copy link
Contributor

jreback commented Jun 20, 2013

this is on purpose, try this for converting from epoch (the docs for this are not fully merged yet):

I have never seen anyone specify epoch as you have an mean Days....what is the use case (most epcoh
is seconds, but usually ms)

In [6]: Series(pd.to_datetime([0,1,2],unit='s'))
Out[6]: 
0   1970-01-01 00:00:00
1   1970-01-01 00:00:01
2   1970-01-01 00:00:02
dtype: datetime64[ns]

I guess could add unit=D as well

In [25]: pd.to_datetime(pd.Series([0,1,2])*86400,unit='s')
Out[25]: 
0   1970-01-01 00:00:00
1   1970-01-02 00:00:00
2   1970-01-03 00:00:00
dtype: datetime64[ns]

@miketkelly
Copy link
Author

In my use case I'm importing data from a SAS data set. SAS stores dates internally as the number of days since 1/1/1960. I could convert that into a string, and from a string into a datetime, but that's far slower than taking the value in as an int, adding a pre-computed number of days to adjust it to 1/1/1970, and then converting that int into a datetime with M8[D]. I can convert to seconds as you suggest, assuming leap seconds will always be ignored.

Thanks for your response. I'll close the case.

@jreback
Copy link
Contributor

jreback commented Jun 20, 2013

@mtkni ok to leave open....will leave this an open issue to add 'D' to units

though pandas/numpy will always be since 1/1/1970 (but that's a diff issue)

feel free to do a PR!

@jreback
Copy link
Contributor

jreback commented Jun 20, 2013

@mtkni we disabled the non-ns datetimetypes (and conversions from) because you can get really weird results ....hopefully units and friends will allow things like this

@jreback
Copy link
Contributor

jreback commented Jun 21, 2013

closed via #3973

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants