IndexError when trying to read_table with pandas

Update: This is a duplicate of usecols with parse_dates and names, "but this question has been answered.


I can't get this code to work for life. As soon as I output the parameter names, it works fine, but it's just plain stupid.

From a space delimited file I want:

  • skip header section
  • import selected columns
  • specify columns
  • parse two columns as date
  • use processed date as index

This almost works:

import panadas as pd
columns = [4, 5, 10, 11, 15, 16, 17, 26, 28, 29]
names = ["DATE","TIME","DLAT", "DLON", "SLAT", "SLON", "SHGT", "HGT", "N", "E"]
ppp_data = pd.read_table(
    filename,
    delim_whitespace=True, # space delimited
    skiprows=8, # skip header rows
    header=None, # don't use first row as column names
    usecols=columns, # only use selected columns
    names=names, # use names for selected columns
    parse_dates=[[4,5]], # join date and time columns and parse as date
    index_col=0, # use parsed date (now column 0) as index
)
print ppp_data

But here is the stack trace I get

Traceback (most recent call last):
  File "plot_squat_test_pandas.py", line 30, in <module>
    index_col=0,
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 400, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 205, in _read
    return parser.read()
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 608, in read
    ret = self._engine.read(nrows)
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 1028, in read
    data = self._reader.read(nrows)
  File "parser.pyx", line 706, in pandas.parser.TextReader.read (pandas/parser.c:6745)
  File "parser.pyx", line 728, in pandas.parser.TextReader._read_low_memory (pandas/parser.c:6964)
  File "parser.pyx", line 804, in pandas.parser.TextReader._read_rows     (pandas/parser.c:7780)
  File "parser.pyx", line 865, in pandas.parser.TextReader._convert_column_data (pandas/parser.c:8512)
  File "parser.pyx", line 1105, in pandas.parser.TextReader._get_column_name (pandas/parser.c:11684)
IndexError: list index out of range

If I comment on the parameter names=namesand it works fine

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 86281 entries, 2013-10-30 00:00:00 to 2013-10-30 23:59:59
Data columns (total 8 columns):
10    86281  non-null values
11    86281  non-null values
15    86281  non-null values
16    86281  non-null values
17    86281  non-null values
26    86281  non-null values
28    86281  non-null values
29    86281  non-null values

What am I missing? Or is it a panadas problem and should I report an error?

python 2.7.3, pandas - 0.12.0. 0.13.0rc1-119-g2485e09 ( ).

+4
2

pandas 0.13.0rc1-119-g2485e09. .

1

usecols, names IndexError

from StringIO import StringIO
import pandas as pd

data = """2013-10-11 11:53:49,1,2,3,4
2013-10-11 11:53:50,1,2,3,4
2013-10-11 11:53:51,1,2,3,4"""

df = pd.read_csv(
    StringIO(data),
    header=None,
    usecols=[0,2,4],
    names=["DATE","COl2","COL4"],
    parse_dates=["DATE"],
    index_col=0,
)
print df

2

rename ,

ppp_data.rename(columns=dict(zip(columns[2:],names)), inplace=True)
+3

names 10 :

In [1]: len(["DATE","TIME","DLAT", "DLON", "SLAT", "SLON", "SHGT", "HGT", "N", "E"])
Out[1]: 10

names, read_table 8 :

Data columns (total 8 columns):

, DataFrame 8 , names 9 ( 8) .

,

parse_dates=[[4,5]],    

4 5 . , 10 , 8 . names 9 , .

+1

All Articles