简体   繁体   English

纪元时间格式到python datetime

[英]epoch time format to python datetime

I am reading data from csv file with pandas.我正在使用熊猫从 csv 文件中读取数据。 The time column is in epoch format which needs to be converted to human readable datetime.时间列是纪元格式,需要转换为人类可读的日期时间。 The dataframe looks like this:数据框如下所示:

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

I tried pd.to_datetime(df['time'], unit='ns') but get OverflowError: Python int too large to convert to C long .我尝试pd.to_datetime(df['time'], unit='ns')但得到OverflowError: Python int too large to convert to C long Could anyone help me how to fix this?谁能帮我解决这个问题? Or is there a better way to convert epoch to datetime?还是有更好的方法将纪元转换为日期时间?

As @Corralien comment, some rows didn't have numeric values and could not be converted.正如@Corralien 评论,某些行没有数值并且无法转换。 I checked them with df[pd.to_numeric(df['time'], errors='coerce').isna()] and excluded them from DataFrame.我用df[pd.to_numeric(df['time'], errors='coerce').isna()]它们,并将它们从 DataFrame 中排除。 Then df['time'] = pd.to_datetime(df['time'], unit='ns') worked fine.然后df['time'] = pd.to_datetime(df['time'], unit='ns')工作正常。

    Epoch
1   1609459009.0
2   1609458998.0
3   1609458963.0
4   1609458893.0
5   1609458770.0
6   1609458685.0
7   1609458551.0
8   1609458465.0
9   1609458432.0
10  1609458369.0

I tried df['time'] = pd.to_datetime(df['time'], unit='ns') but it doesnt work.我试过df['time'] = pd.to_datetime(df['time'], unit='ns')但它不起作用。 Keeps saying KeyError: 'time' I am trying to convert the epoch date column to python datetime but it doesnt seem to work.一直说 KeyError: 'time' 我正在尝试将纪元日期列转换为 python 日期时间,但它似乎不起作用。 I am reading the file as a csv and trying to convert the times.我正在以 csv 格式读取文件并尝试转换时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM