简体   繁体   English

在一行的 dataframe 中将字符串转换为日期时间

[英]Converting strings to datetime in dataframe of one row

I have a CSV file that serves as an input to my calculations.我有一个 CSV 文件作为我计算的输入。 The file contains a table that is read as a dataframe in my script.该文件包含一个表,在我的脚本中被读取为 dataframe。 Two columns are datetime objects.两列是日期时间对象。 When I am reading the file to a dataframe I am converting those values from default string type to datatime applying to_datetime function with UTC argument.当我将文件读取到 dataframe 时,我将这些值从默认字符串类型转换为数据时间,应用 to_datetime function 和 UTC 参数。 The code is below:代码如下:

import pandas as pd

df_in = pd.read_csv('./Out/in.csv', index_col = 0, usecols = [0, 1, 2, 3, 4], header = 0)

df_in.iloc[:,2:4] = df_in.iloc[:,2:4].apply(pd.to_datetime, utc = True)

print(df_in)

Works like a charm on the following input CSV:对以下输入 CSV 非常有效:

,Rig ID,Rig Name,Start Time,End Time
60,5,D004,2020-05-08 02:45:14.664341,2020-05-10 08:12:29.719839
70,5,D004,2020-05-14 07:59:09.280761,2020-05-15 02:11:52.358921

The moment the CSV file is reduced to 1 line, eg: CSV 文件减少到 1 行的那一刻,例如:

,Rig ID,Rig Name,Start Time,End Time
60,5,D004,2020-05-08 02:45:14.664341,2020-05-10 08:12:29.719839

I am getting the error我收到错误

TypeError: data type not understood.

As far as I can understand it is something to do with slice assignment on single line dataframe据我所知,这与单行 dataframe 上的切片分配有关

df_in.iloc[:,2:4] =

but I am struggling to find its cause and "smart" workaround.但我正在努力寻找其原因和“智能”解决方法。

Appreciate your help!感谢你的帮助!

I've found that with older versions of pandas this can happen with when date fields are present in the data frame (specifically datetime64 variants).我发现对于 pandas 的旧版本,当数据框中存在日期字段(特别是 datetime64 变体)时,可能会发生这种情况。 So, instead of iloc try this...所以,而不是iloc试试这个......

df_in[['Start Time','End Time']] = df_in[['Start Time','End Time']].apply(pd.to_datetime, utc = True)

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

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