3
$\begingroup$

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
$\endgroup$

3 Answers 3

4
$\begingroup$

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

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

cheers.

$\endgroup$
1
  • $\begingroup$ Please try my alternative, and mark it if it solves your problem. $\endgroup$
    – Emre
    Aug 22, 2017 at 8:15
4
$\begingroup$

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
$\endgroup$
2
  • $\begingroup$ just say your answer - great. Thank you. $\endgroup$
    – diogenes
    Jun 17, 2020 at 3:36
  • $\begingroup$ note that the view method throws a TypeError if the Series is empty, while the astype method does not; just in case you might need to deal with that in your data $\endgroup$ Jul 15, 2021 at 12:07
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.