TransWikia.com

pandas datetime to Unix

Data Science Asked by diogenes on May 16, 2021

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

3 Answers

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

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

cheers.

Answered by diogenes on May 16, 2021

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

Answered by Emre on May 16, 2021

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.

Answered by Nick Klavohn on May 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP