使用 pandas read_csv解析时间戳时出错 [英] Error when parsing timestamp with pandas read_csv

查看:116
本文介绍了使用 pandas read_csv解析时间戳时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载一个csv文件,其格式如下:

I am trying to load a csv file which is formatted as follow :

                  40010  40015  40020  40025  40030  40035  40040  40045  
2008-11-03 00:00    786    212    779    227    220    131    680   1006   
2008-11-03 00:03    760    200    765    234    225    133    694   1063   
2008-11-03 00:06    757    205    769    237    230    136    726   1051   
2008-11-03 00:09    781    207    765    240    235    137    711   1040   
2008-11-03 00:12    759    203    751    232    225    134    717   1088
...

文件以逗号分隔.这里没有固定宽度.

我希望行索引为日期时间,所以这是我当前在加载文件时正在做的事情:

I want the rows indexes to be datetime so here's what i'm currently doing when loading the file :

def dateparse (timestamp):   
    return datetime.datetime.strptime(timestamp, '%Y-%m-%d %I:%M')

global_data_train = pd.read_csv('RTAHistorical.csv', sep=",",parse_dates=True, date_parser=dateparse, header=0, index_col=0, skip_blank_lines = True, engine='python')

但是我遇到了以下错误:

But i'm getting the following error :

TypeError: strptime() argument 1 must be str, not numpy.ndarray

正如我所见有人使用同样的方法成功了,我不太明白这个错误.

As I have seen some people using the very same method successfully, I don't quite understand this error.

我在做什么错了?

推荐答案

对我来说,将格式更改为%Y-%m-%d %H:%M:

For me works change format to %Y-%m-%d %H:%M:

def dateparse (timestamp):   
    return pd.datetime.strptime(timestamp, '%Y-%m-%d %H:%M')

示例:

import pandas as pd
from pandas.compat import StringIO

temp=u"""40010,40015,40020,40025,40030,40035,40040,40045
2008-11-03 00:00,786,212,779,227,220,131,680,1006
2008-11-03 00:03,760,200,765,234,225,133,694,1063
2008-11-03 00:06,757,205,769,237,230,136,726,1051
2008-11-03 00:09,781,207,765,240,235,137,711,1040
2008-11-03 00:12,759,203,751,232,225,134,717,1088"""
#after testing replace StringIO(temp) to filename
def dateparse (timestamp):   
    return pd.datetime.strptime(timestamp, '%Y-%m-%d %H:%M')

global_data_train = pd.read_csv(StringIO(temp), 
                                sep=",", 
                                parse_dates=True, 
                                date_parser=dateparse, 
                                header=0, 
                                index_col=0, 
                                skip_blank_lines = True, 
                                engine='python')
print (global_data_train)
                     40010  40015  40020  40025  40030  40035  40040  40045
2008-11-03 00:00:00    786    212    779    227    220    131    680   1006
2008-11-03 00:03:00    760    200    765    234    225    133    694   1063
2008-11-03 00:06:00    757    205    769    237    230    136    726   1051
2008-11-03 00:09:00    781    207    765    240    235    137    711   1040
2008-11-03 00:12:00    759    203    751    232    225    134    717   1088

print (global_data_train.index)
DatetimeIndex(['2008-11-03 00:00:00', '2008-11-03 00:03:00',
               '2008-11-03 00:06:00', '2008-11-03 00:09:00',
               '2008-11-03 00:12:00'],
              dtype='datetime64[ns]', freq=None)

也可以省略date_parser=dateparse.

import pandas as pd
from pandas.compat import StringIO

temp=u"""40010,40015,40020,40025,40030,40035,40040,40045
2008-11-03 00:00,786,212,779,227,220,131,680,1006
2008-11-03 00:03,760,200,765,234,225,133,694,1063
2008-11-03 00:06,757,205,769,237,230,136,726,1051
2008-11-03 00:09,781,207,765,240,235,137,711,1040
2008-11-03 00:12,759,203,751,232,225,134,717,1088"""
#after testing replace StringIO(temp) to filename
global_data_train = pd.read_csv(StringIO(temp), 
                                parse_dates=True, 
                                skip_blank_lines = True)
print (global_data_train)
                     40010  40015  40020  40025  40030  40035  40040  40045
2008-11-03 00:00:00    786    212    779    227    220    131    680   1006
2008-11-03 00:03:00    760    200    765    234    225    133    694   1063
2008-11-03 00:06:00    757    205    769    237    230    136    726   1051
2008-11-03 00:09:00    781    207    765    240    235    137    711   1040
2008-11-03 00:12:00    759    203    751    232    225    134    717   1088

print (global_data_train.index)
DatetimeIndex(['2008-11-03 00:00:00', '2008-11-03 00:03:00',
               '2008-11-03 00:06:00', '2008-11-03 00:09:00',
               '2008-11-03 00:12:00'],
              dtype='datetime64[ns]', freq=None)

这篇关于使用 pandas read_csv解析时间戳时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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