Reputation: 157
So I'm trying to convert the 'Event Time' (Time in microseconds since 1970-01-01 00:00:00 UTC) column in a pandas data frame to human readable date & time.
Name of column: df['Event Time']
input: df['Event Time'].iloc[0]
output: 1519952249827533
I need a script to transform the entire column.
I tried the following, but there needs to be something much more simple:
from datetime import datetime, timedelta
epoch = datetime(1970, 1, 1)
cookie_microseconds_since_epoch = df['Event Time'].iloc[0]
cookie_datetime = epoch + timedelta(microseconds=cookie_microseconds_since_epoch)
str(cookie_datetime)
Thank you!
Upvotes: 1
Views: 4296
Reputation: 294488
Use pd.to_datetime
and specify the unit
as microseconds. Mind that the microseconds unit is us
(I didn't know that a few minutes ago (-:)
df['Event Time'] = pd.to_datetime(df['Event Time'], unit='us')
df = pd.DataFrame({'Event Time': [1519952249827533]})
df['Event Time'] = pd.to_datetime(df['Event Time'], unit='us').dt.floor('s')
df
Event Time
0 2018-03-02 00:57:29
If you wanted a string result instead of Timestamps
use strftime
pd.to_datetime(df['Event Time'], unit='us').dt.strftime('%Y-%m-%d %H:%M:%S')
0 2018-03-02 00:57:29
Name: Event Time, dtype: object
See strftime.org for more info on using strftime.
Upvotes: 3