python - 纪元时间格式到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
. 谁能帮我解决这个问题?还是有更好的方法将纪元转换为日期时间?
解决方案
正如@Corralien 评论,某些行没有数值并且无法转换。我检查了它们df[pd.to_numeric(df['time'], errors='coerce').isna()]
并将它们从 DataFrame 中排除。然后df['time'] = pd.to_datetime(df['time'], unit='ns')
工作正常。
推荐阅读
- ios - 为什么我不能为我的 App Store Connect 添加新的银行账户?
- algorithm - 在最大流量问题中,如何找到提供最大流量的所有可能路径集?
- r - 从 SparkR ML RF 中提取向量概率而不将 SparkDataFrame 强制转换为 R data.frame
- python-3.x - 模块日志记录的属性 funcName 返回一个默认的 Levelname
- python - python中大型数据集的多处理(查找重复项)
- css - css预处理器的less文件样式中~的符号是什么意思
- php - 如何在php中合并选择选项名称
- azure - Azure 应用程序网关能否将请求分发到特定 URL?
- sql - 在可能“领先”或“落后”的情况下使用案例
- javascript - 后台同步未在服务人员中触发事件