pandas错误:找到作为索引的切片?

时间:2015-07-25 13:57:29

标签: python pandas

下面的代码打破了最简单的行,我认为我只是添加一个名为'year'的列,其中包含year变量中的常量值。我的套餐是最新的Anaconda 2.3。

为什么这个索引错误?

代码:

# -*- coding: utf-8 -*-
import iopro
from pandas import *

for year in xrange(2005,2013):
    for month in xrange(1,13):
        if year == 2005 and month < 7:
            continue
        filename = 'SOMEPATH' + str(year) + '_mon'+ str(month) +'.txt'
        adapter = iopro.text_adapter(filename,parser='csv',field_names=True,output='dataframe',delimiter='\t')
        monthly = adapter[['var1','var2','var3']][:]
        monthly['year']=year

错误消息是:

Traceback (most recent call last):
 File "drugs.py", line 21, in <module>
   monthly['year']=year
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2127, in __setitem__
   self._set_item(key, value)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2205, in _set_item
   NDFrame._set_item(self, key, value)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 1196, in _set_item
   self._data.set(key, value)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/internals.py", line 2980, in set
   loc = self.items.get_loc(item)
 File "/home/seidav/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1572, in get_loc
   return self._engine.get_loc(_values_from_object(key))
 File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)
TypeError: expected string or Unicode object, slice found

0 个答案:

没有答案