Reputation: 451
I am converting a datetime[ns] column to a different timezone and the resulting column looks like this:
0 2011-05-25 20:30:00+02:00
1 2011-05-24 20:30:00+02:00
2 2011-05-20 20:30:00+02:00
3 2011-05-19 20:30:00+02:00
4 2011-05-15 13:30:00+02:00
I am trying to pull out the offset information, here +02:00
.
offset = converted.apply(lambda x: x.strftime('%z'))
But it gives me a blank column.
When the column is of dtype == object/str, the offest information disappears so can't put it out with regex either.
Any ideas?
Upvotes: 1
Views: 282
Reputation: 862591
I think you need strftime
:
print (df)
date
0 2011-05-25 20:30:00+02:00
1 2011-05-24 20:30:00+02:00
2 2011-05-20 20:30:00+02:00
3 2011-05-19 20:30:00+02:00
4 2011-05-15 13:30:00+02:00
print (df.date.dt.tz)
Europe/Bratislava
df['timezone'] = df.date.dt.strftime('%z')
print (df)
date timezone
0 2011-05-25 20:30:00+02:00 +0200
1 2011-05-24 20:30:00+02:00 +0200
2 2011-05-20 20:30:00+02:00 +0200
3 2011-05-19 20:30:00+02:00 +0200
4 2011-05-15 13:30:00+02:00 +0200
If dtype
of date
column is object/string
use indexing with str:
df['date'] = df.date.astype(str)
df['timezone'] = df.date.str[-6:]
print (df)
date timezone
0 2011-05-25 20:30:00+02:00 +02:00
1 2011-05-24 20:30:00+02:00 +02:00
2 2011-05-20 20:30:00+02:00 +02:00
3 2011-05-19 20:30:00+02:00 +02:00
4 2011-05-15 13:30:00+02:00 +02:00
Upvotes: 1