还有一个question那是十一岁,有类似的头衔。
我有一个带有一列 datetime.time 值的 pandas 数据框。
val time
a 12:30:01.323
b 12:48:04.583
c 14:38:29.162
我想将时间列从 UTC 转换为 EST。
我尝试执行 dataframe.tz_localize('utc').tz_convert('US/Eastern')
但它给了我以下错误:RangeIndex Object has no attribute tz_localize
最佳答案
tz_localize
和 tz_convert
处理 DataFrame 的索引。因此,您可以执行以下操作:
- 将“时间”转换为时间戳格式
- 将“时间”列设置为索引并使用转换函数
reset_index()
- 只保留时间
尝试:
dataframe["time"] = pd.to_datetime(dataframe["time"],format="%H:%M:%S.%f")
output = (dataframe.set_index("time")
.tz_localize("utc")
.tz_convert("US/Eastern")
.reset_index()
)
output["time"] = output["time"].dt.time
>>> output
time val
0 15:13:12.349211 a
1 15:13:13.435233 b
2 15:13:14.345233 c
关于python - 如何将 Pandas 日期时间列从 UTC 转换为 EST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71444095/