NinjaGaiden
NinjaGaiden

Reputation: 3146

pandas converting datetime to epoch with correct timezone

I have a simple dataframe like this

                Started
job
446 2016-09-29 08:53:24
447 2016-09-29 08:54:37
448 2016-09-29 08:55:49
449 2016-09-29 08:57:00
450 2016-09-29 08:58:12

I want to convert this datetime to an epoch with correct timezone. So I am doing

df['started_epoch']=df['Started'].astype(np.int64)//10**9

This works but the epoch i am getting is in UTC. How can I get EDT?

Upvotes: 1

Views: 551

Answers (1)

jezrael
jezrael

Reputation: 862741

I think you need tz_localize:

#dtype of Started is datetime
df['started_epoch'] = df.Started.dt.tz_localize('EST5EDT').astype(np.int64)//10**9
print (df)
   job             Started  started_epoch
0  446 2016-09-29 08:53:24     1475153604
1  447 2016-09-29 08:54:37     1475153677
2  448 2016-09-29 08:55:49     1475153749
3  449 2016-09-29 08:57:00     1475153820
4  450 2016-09-29 08:58:12     1475153892

I try test it and it seems it works:

df['started_epoch'] = df.Started.dt.tz_localize('EST5EDT').astype(np.int64)//10**9
df['started_epoch1'] = df.Started.astype(np.int64)//10**9
print (df)
   job             Started  started_epoch  started_epoch1
0  446 2016-09-29 08:53:24     1475153604      1475139204
1  447 2016-09-29 08:54:37     1475153677      1475139277
2  448 2016-09-29 08:55:49     1475153749      1475139349
3  449 2016-09-29 08:57:00     1475153820      1475139420
4  450 2016-09-29 08:58:12     1475153892      1475139492

Upvotes: 1

Related Questions