PolarBear10
PolarBear10

Reputation: 2305

Efficient way to Convert UTC time to Epoch in millisecond in Python in a Pandas DataFrame

How to Convert UTC time to epoch in a python DataFrame in an efficient manner ?

Here is what I have:

df = pd.DataFrame(['2017-12-12T00:59:54', '2017-12-12T01:09:52', '2017-12-12T01:19:51'], columns = ['time'])

Here is what I did:

import datetime as dt

epoch = []
for x in df['time']:
    dt = datetime.strptime(x, '%Y-%m-%dT%H:%M:%S')
    epoch.append(dt.replace(tzinfo=timezone.utc).timestamp())

df['epoch'] = epoch

Here is my Issue:

This for loop seems to be very slow in a large pandas data frame, is there a more efficient way to do it?

Thank you! :)

Upvotes: 0

Views: 555

Answers (1)

jezrael
jezrael

Reputation: 862741

For me working:

df['epoch'] = pd.to_datetime(df['time']).astype(np.int64) // 10**9
print (df)

                  time       epoch
0  2017-12-12T00:59:54  1513040394
1  2017-12-12T01:09:52  1513040992
2  2017-12-12T01:19:51  1513041591

Upvotes: 2

Related Questions