2019-02-28 06:15 +08:00
我试图将这个字符串转换为日期, 它应该等于 2019-02-27 22:15
,因为我想减去8小时。我该怎么做?
import pandas as pd
pd.Timestamp('2019-02-28 06:15 +08:00').tz_convert("UTC") # Timestamp('2019-02-27 22:15:00+0000', tz='UTC')
使用tz=None
,我们可以通过Timestamp.tz_convert
将时区转换为UTC(如果需要),并删除时区信息,如果需要将值转换为日期时间:
a = '2019-02-28 06:15 +08:00'
b = pd.to_datetime(a).tz_convert(None)
print (b)
2019-02-27 22:15:00
如果DataFrame中的列使用Series.dt.tz_convert
:
df = pd.DataFrame({'date':['2019-02-28 06:15 +08:00','2019-02-28 06:15 +08:00']})
df['date'] = pd.to_datetime(df['date']).dt.tz_convert(None)
print (df)
date
0 2019-02-27 22:15:00
1 2019-02-27 22:15:00
import pandas as pd
d = '2019-02-28 06:15 +08:00'
pd.to_datetime(d).tz_convert('UTC')
Timestamp('2019-02-27 22:15:00+0000', tz='UTC')