【发布时间】:2019-11-22 07:59:18
【问题描述】:
我收到包含时间戳列和 UTC 偏移列的每日报告。使用 pandas,我可以将 int Timestamp 转换为 datetime64 类型。不幸的是,我不知道如何使用偏移量。
由于“UTC 偏移”列作为字符串出现,我尝试将其转换为 int 以提供帮助,但不知道如何使用它。我尝试使用 pd.offsets.Hour,但不能使用偏移列。
df = pd.read_csv(filename, encoding='utf-8', delimiter=r'\t',engine='python')
df['Timestamp'] = pd.to_datetime(df[r'Stream Timestamp'],utc=True, unit='s')
print(df[:3][r'Stream Timestamp'])
0 2019-05-01 14:21:37+00:00
1 2019-05-01 15:50:12+00:00
Name: Stream Timestamp, dtype: datetime64[ns, UTC]
0 -06:00
1 +01:00
2 -04:00
Name: UTC Offset, dtype: object
df[r"UTC Offset"] = df[r"UTC Offset"].astype(int)
理想情况下,我想做这样的事情
df[r'Adjusted'] = df[r'Timestamp'] + pd.offsets.Hour(df[r'UTC Offset'])
但是我似乎无法弄清楚如何最好地引用偏移列。一般来说,我对 datetime 有点陌生,但我们将不胜感激!
【问题讨论】:
标签: python pandas datetime utc timezone-offset