首页 > 解决方案 > 纪元时间格式到python datetime

问题描述

我正在使用熊猫从 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. 谁能帮我解决这个问题?还是有更好的方法将纪元转换为日期时间?

标签: pythonpandasepoch

解决方案


正如@Corralien 评论,某些行没有数值并且无法转换。我检查了它们df[pd.to_numeric(df['time'], errors='coerce').isna()]并将它们从 DataFrame 中排除。然后df['time'] = pd.to_datetime(df['time'], unit='ns')工作正常。


推荐阅读