使用 pandas to_datetime时如何定义格式? [英] How to define format when using pandas to_datetime?

查看:77
本文介绍了使用 pandas to_datetime时如何定义格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据具有以下格式的 testresult.csv 文件绘制RESULT与TIME的关系图,我很难正确定义TIME列的数据类型。


I want to plot RESULT vs TIME based on a testresult.csv file that has following format, and I have trouble to get the TIME column's datatype defined properly.

TIME,RESULT  
03/24/2016 12:27:11 AM,2  
03/24/2016 12:28:41 AM,76  
03/24/2016 12:37:23 AM,19  
03/24/2016 12:38:44 AM,68  
03/24/2016 12:42:02 AM,44  
...

要读取csv文件,这是我编写的代码:
raw_df = pd.read_csv('testresult.csv',index_col = None,parse_dates = ['TIME'],infer_datetime_format = True)

此代码有效,但速度非常慢,并且我认为 infer_datetime_format 需要时间。因此,我尝试首先默认读取csv,然后使用 to_datetime()将对象dtype'TIME'转换为datetime dtype,希望通过定义格式,这可能会加快速度。

To read the csv file, this is the code I wrote: raw_df = pd.read_csv('testresult.csv', index_col=None, parse_dates=['TIME'], infer_datetime_format=True)
This code works, but it is extremely slow, and I assume that the infer_datetime_format takes time. So I tried to read in the csv by default first, and then convert the object dtype 'TIME' to datetime dtype by using to_datetime(), and I hope by defining the format, it might expedite the speed.

raw_df =  pd.read_csv('testresult.csv')
raw_df.loc['NEWTIME'] = pd.to_datetime(raw_df['TIME'], format='%m/%d%Y %-I%M%S %p')

此代码抱怨错误:


" ValueError:'-'是错误的指令格式'%m /%d%Y%-I%M%S%p'''


推荐答案

您传递的格式无效。 I 之间的破折号不应存在。

The format you are passing is invalid. The dash between the % and the I is not supposed to be there.

df['TIME'] = pd.to_datetime(df['TIME'], format="%m/%d/%Y %I:%M:%S %p")

这将转换您的时间列为日期时间。

This will convert your TIME column to a datetime.

或者,您可以调整 read_csv 为此调用:

Alternatively, you can adjust your read_csv call to do this:

pd.read_csv('testresult.csv', parse_dates=['TIME'], 
    date_parser=lambda x: pd.to_datetime(x, format='%m/%d/%Y %I:%M:%S %p'))

同样,它使用适当的格式,没有多余的-,但它也会传入格式为 date_parser 参数,而不是让熊猫尝试使用 infer_datetime_format 参数来猜测它。

Again, this uses the appropriate format with out the extra -, but it also passes in the format to the date_parser parameter instead of having pandas attempt to guess it with the infer_datetime_format parameter.

这篇关于使用 pandas to_datetime时如何定义格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆