pandas read_csv导入结果错误 [英] Pandas read_csv import results in error
问题描述
我的csv如下(MQM Q.csv):
My csv is as follows (MQM Q.csv):
Date-Time,Value,Grade,Approval,Interpolation Code
31/08/2012 12:15:00,,41,1,1
31/08/2012 12:30:00,,41,1,1
31/08/2012 12:45:00,,41,1,1
31/08/2012 13:00:00,,41,1,1
31/08/2012 13:15:00,,41,1,1
31/08/2012 13:30:00,,41,1,1
31/08/2012 13:45:00,,41,1,1
31/08/2012 14:00:00,,41,1,1
31/08/2012 14:15:00,,41,1,1
前几行没有值"条目,但稍后开始.
The first few lines have no "Value" entries but they start later on.
这是我的代码:
import pandas as pd
from StringIO import StringIO
Q = pd.read_csv(StringIO("""/cygdrive/c/temp/MQM Q.csv"""), header=0, usecols=["Date-Time", "Value"], parse_dates=True, dayfirst=True, index_col=0)
我收到以下错误:
Traceback (most recent call last):
File "daily.py", line 4, in <module>
Q = pd.read_csv(StringIO("""/cygdrive/c/temp/MQM Q.csv"""), header=0, usecols=["Date-Time", "Value"], parse_dates=True, dayfirst=True, index_col=0)
File "/usr/lib/python2.7/site-packages/pandas-0.14.0-py2.7-cygwin-1.7.30-x86_64.egg/pandas/io/parsers.py", line 443, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/lib/python2.7/site-packages/pandas-0.14.0-py2.7-cygwin-1.7.30-x86_64.egg/pandas/io/parsers.py", line 228, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/lib/python2.7/site-packages/pandas-0.14.0-py2.7-cygwin-1.7.30-x86_64.egg/pandas/io/parsers.py", line 533, in __init__
self._make_engine(self.engine)
File "/usr/lib/python2.7/site-packages/pandas-0.14.0-py2.7-cygwin-1.7.30-x86_64.egg/pandas/io/parsers.py", line 670, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/lib/python2.7/site-packages/pandas-0.14.0-py2.7-cygwin-1.7.30-x86_64.egg/pandas/io/parsers.py", line 1067, in __init__
col_indices.append(self.names.index(u))
ValueError: 'Value' is not in list
推荐答案
这似乎是csv解析器的错误,首先有效:
This appears to be a bug with the csv parser, firstly this works:
df = pd.read_csv('MQM Q.csv')
这也有效:
df = pd.read_csv('MQM Q.csv', usecols=['Value'])
但是如果我想要Date-Time
,它将失败,并显示与您相同的错误消息.
but if I want Date-Time
then it fails with the same error message as yours.
所以我注意到它是utf-8编码的,所以我使用notepad ++转换为ANSI并起作用,然后我尝试了没有BOM的utf-8,它也起作用了.
So I noticed it was utf-8 encoded and so I converted using notepad++ to ANSI and it worked, I then tried utf-8 without BOM and it also worked.
然后我将其转换为utf-8(大概现在有一个BOM),并且失败了,并且错误与以前相同,所以我不认为您现在正在对此进行成像,这看起来像是一个错误.
I then converted it to utf-8 (presumably there is now a BOM) and it failed with the same error as before, so I don't think you are imaging this now and this looks like a bug.
我正在使用python 3.3,pandas 0.14和numpy 1.8.1
I am using python 3.3, pandas 0.14 and numpy 1.8.1
要解决此问题,请执行以下操作:
To get around this do this:
df = pd.read_csv('MQM Q.csv', usecols=[0,1], parse_dates=True, dayfirst=True, index_col=0)
这会将您的索引设置为Date-Time列,该列将正确转换为datetimeindex.
This will set your index to the Date-Time column which will correctly convert to a datetimeindex.
In [40]:
df.index
Out[40]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-08-31 12:15:00, ..., 2013-11-28 10:45:00]
Length: 43577, Freq: None, Timezone: None
这篇关于 pandas read_csv导入结果错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!