I have a dataframe with a datetime object as below (GMT)
blah dateuno dateduo
a 29/06/2020 13:27 29/06/2020 17:44
b 19/05/2020 11:15 19/05/2020 19:25
c 8/06/2020 11:50 8/06/2020 19:51
d 9/06/2020 11:37 9/06/2020 19:38
e 20/07/2020 11:44 20/07/2020 19:47
f 21/07/2020 11:02 21/07/2020 19:08
g 23/07/2020 11:01 23/07/2020 19:05
How can i convert these datetimes to penssylvania times as here https://www.timeanddate.com/time/zone/usa/pennsylvania
through the entire column of this dataframe?
asked Aug 13, 2020 at 15:10
LeCoda
1,0677 gold badges49 silver badges92 bronze badges
1 Answer 1
judging from the format in your sample, you need to cast to_datetime first, with kwargs dayfirst=True and utc=True (to correctly describe the input). you can then tz_convert to the appropriate time zone. Ex:
df['dateuno'] = pd.to_datetime(df['dateuno'], dayfirst=True, utc=True)
df['dateduo'] = pd.to_datetime(df['dateduo'], dayfirst=True, utc=True)
df['dateuno'] = df['dateuno'].dt.tz_convert('US/Eastern')
df['dateduo'] = df['dateduo'].dt.tz_convert('US/Eastern')
# df
# blah dateuno dateduo
# 0 a 2020年06月29日 09:27:00-04:00 2020年06月29日 13:44:00-04:00
# 1 b 2020年05月19日 07:15:00-04:00 2020年05月19日 15:25:00-04:00
# 2 c 2020年06月08日 07:50:00-04:00 2020年06月08日 15:51:00-04:00
# 3 d 2020年06月09日 07:37:00-04:00 2020年06月09日 15:38:00-04:00
# 4 e 2020年07月20日 07:44:00-04:00 2020年07月20日 15:47:00-04:00
# 5 f 2020年07月21日 07:02:00-04:00 2020年07月21日 15:08:00-04:00
# 6 g 2020年07月23日 07:01:00-04:00 2020年07月23日 15:05:00-04:00
answered Aug 13, 2020 at 19:02
FObersteiner
26.1k9 gold badges60 silver badges94 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py