【发布时间】:2021-10-31 18:54:44
【问题描述】:
我正在使用 pandas 从 csv 文件中读取数据。时间列是纪元格式,需要转换为人类可读的日期时间。数据框如下所示:
name time rCurrentL1 rCurrentL2 rCurrentL3
0 Assembler_No1BC1_ElecData 1623691319004000000 0.0032874299213290215 0.0021221311762928963 0.00042045069858431816
1 Assembler_No1BC1_ElecData 1623691319005000000 0.0032874299213290215 0.0021221311762928963 0.00042045069858431816
2 Assembler_No1BC1_ElecData 1623691319006000000 0.0032874299213290215 0.0021221311762928963 0.00042045069858431816
我尝试了pd.to_datetime(df['time'], unit='ns'),但得到了OverflowError: Python int too large to convert to C long。谁能帮我解决这个问题?或者有没有更好的方法将纪元转换为日期时间?
【问题讨论】:
-
你的数据显示我运行良好,你的 pandas 版本是多少?
-
pd.to_datetime(df['time'] / 1000000, unit='ms')的输出是什么 -
检查是否有大于 pd.Timestamp.max 的值:
df['time'].gt(pd.Timestamp.max.value).sum() -
@jezrael pandas 版本:1.2.4
-
时间列中的所有行都不包含数字。使用
df[pd.to_numeric(df['time'], errors='coerce').isna()]找到它们并修复。