IndexError:单个位置索引器使用pandas Series.from_csv是超出范围的

时间:2017-12-18 17:49:37

标签: python pandas time-series

我收到错误

  File "C:/Users/Toshiba/PycharmProjects/untitled1/stationarity.py", line 7, in <module>

    series = Series.from_csv('flowcaida.csv')

  File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\series.py", line 2640, in from_csv

    result = df.iloc[:, 0]

  File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1325, in __getitem__

    return self._getitem_tuple(key)

  File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1662, in _getitem_tuple

    self._has_valid_tuple(tup)
  File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 189, in _has_valid_tuple

    if not self._has_valid_type(k, i):

  File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1597, in _has_valid_type
    return self._is_valid_integer(key, axis)

  File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1638, in _is_valid_integer

    raise IndexError("single positional indexer is out-of-bounds")

IndexError: single positional indexer is out-of-bounds

使用:

series = Series.from_csv('flowcaida.csv')

这是我的数据:

651680

459151

596299

474950

631421

465275

629861

455582

650192

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

Series.from_csv已被弃用了相当长一段时间。您应该使用read_csv代替。

由于您有一个列CSV,因此您可以传递squeeze=True,以便返回Series

s = pd.read_csv(buf, header=None, names=["Data"], squeeze=True)
s

0    651680
1    459151
2    596299
3    474950
4    631421
5    465275
6    629861
7    455582
8    650192
Name: Data, dtype: int64

type(s)
pandas.core.series.Series