1

In pandas, how to convert a series of Timestamp in Unix timestamp?

enter image description here

xeon123
  • 819
  • 1
  • 10
  • 25

1 Answers1

1

First, see how it works in python:

from datetime import datetime
date_str = '2019-12-16 12:39:57.633000'
date = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f')
date.timestamp()

and you'll get 1576517997.633 #note the time now is in epoch such that:

GMT: Monday, December 16, 2019 5:39:57.633 PM
Your time zone: Monday, December 16, 2019 12:39:57.633 PM GMT-05:00

Seconds, in pandas it works (almost) the same:

df = pd.DataFrame({'DateTime':['2019-12-16 12:39:57.633000']})
df['DateTime']=  pd.to_datetime(df['DateTime'])
df['epoch'] = df['DateTime'].apply(lambda x: x.timestamp())
adhg
  • 10,437
  • 12
  • 58
  • 94