pandas datetime to Unix

3

I need to convert a datetime.date(2007, 4, 30) to a Unix timestamp in pandas.

thank you.

my datable:

0     2007-04-30
1     2007-05-31
2     2007-06-30
3     2007-07-31
4     2007-08-31
Name: datetime, dtype: object

diogenes

Posted 2017-05-24T04:23:45.173

Reputation: 81

Answers

4

finally worked something out though I wonder if it is the best solution?

dt = pd.DatetimeIndex ( dt ).astype ( np.int64 )/1000000

cheers.

diogenes

Posted 2017-05-24T04:23:45.173

Reputation: 81

Please try my alternative, and mark it if it solves your problem. – Emre – 2017-08-22T08:15:21.853

4

Use the view method (docs):

from pandas import Series
from datetime import date
df = Series([date(2007,4,30), date(2007,5,31), date(2007,6,30), date(2007,7,31), date(2007,8,31)], dtype='datetime64')
df.view('int64')

The output is:

0    1177891200000000000
1    1180569600000000000
2    1183161600000000000
3    1185840000000000000
4    1188518400000000000
dtype: int64

Emre

Posted 2017-05-24T04:23:45.173

Reputation: 9 953

just say your answer - great. Thank you. – diogenes – 2020-06-17T03:36:38.527

0

These are the fastest two methods I've found, displayed with their respective runtimes. Results were averaged over 1000 iterations using a series with a length of about 30k.

df['date'].view('int64')

Avg. Runtime: 58us

df['date'].astype(np.int64)

Avg. Runtime: 98us

Note 1: Depending on the architecture of your machine (32-bit vs 64-bit), pandas and numpy may store times in different units. I've seen comments about behavior changing between versions.

Note 2: When dealing with large datasets, consider using unsigned integers since your times will always be positive. Also, you may not need nanosecond precision, in which case converting to seconds may allow further downcasting.

Nick Klavohn

Posted 2017-05-24T04:23:45.173

Reputation: 1