0

consider the below sample pandas DataFrame

df = pd.DataFrame({'date':[pd.to_datetime('2016-08-11 14:09:57.00'),pd.to_datetime('2016-08-11 15:09:57.00'),pd.to_datetime('2016-08-11 16:09:57.8700')]})

I can convert single instance into np.int64 type with

print(df.date[0].value)
1470924597000000000

or convert the entire columns iteratively with

df.date.apply(lambda x: x.value)

How can I achieve this without using iteration? something like

df.date.value

I would also want to convert back the np.int64 object to pd.Timestamp object without using iteration. I got some insights from solutions posted here and here but it doesn't solve my problem.

Siraj S.
  • 3,481
  • 3
  • 34
  • 48
  • 1
    `df['date'].astype('int64')` ? – anky Aug 05 '20 at 14:02
  • Did you check this solution: https://stackoverflow.com/questions/16852911/how-do-i-convert-dates-in-a-pandas-data-frame-to-a-date-data-type – Jorge Aug 05 '20 at 14:07
  • @anky/@Jorge i was grappling with this for the whole afternoon !!. This solves my problem. thanks a lot. – Siraj S. Aug 05 '20 at 14:13
  • Does this answer your question? [pandas convert from datetime to integer timestamp](https://stackoverflow.com/questions/54312802/pandas-convert-from-datetime-to-integer-timestamp) – FObersteiner Aug 05 '20 at 14:59

1 Answers1

0

as per @anky comments above, the solution was straightforward.

df.date.astype('int64') >> to int64 dtype
pd.to_datetime(df.date) >> from int dtype to datetime64 dtype
Siraj S.
  • 3,481
  • 3
  • 34
  • 48