question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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]'))

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jrebackcommented, 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]
0reactions
jrebackcommented, Jun 21, 2013

closed via #3973

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with converting a pandas column from int64 to datetime64
dtypes is int64 . That's after I used pd. to_datetime(dataset. Year, format='%Y') to convert the column from int64 to datetime64.
Read more >
pandas.Series.astype — pandas 1.5.2 documentation
Cast a pandas object to a specified dtype dtype . Parameters. dtypedata type, or dict of column name -> data type. Use a...
Read more >
Pandas Change String to Date in DataFrame
Pandas change or convert DataFrame Column Type From String to Date type datetime64 Format - You can change the pandas DataFrame column type ......
Read more >
Source code for matplotlib.dates
See :mod:`matplotlib.ticker` for general information on tick locators and ... convert from datetime64 to datetime: dt = dt.tolist() # datetime64 is always ...
Read more >
Convert the column type from string to datetime format in ...
We cannot perform any time series based operation on the dates if they are ... Now we will convert it to datetime format...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found