pandas DataFrame元数据和set_index

时间:2015-04-01 11:16:57

标签: python-2.7 pandas

在为pandas DataFrame编写好的元数据处理程序几个小时之后,突然间:

>>> import pandas as pd
>>> a = pd.DataFrame({'frame':[0,1,2,3,4,5,6,7], 'x':[1,2,2,2,3,4,2,11], 'y':[2,2,3,2,2,3,2,10]})
>>> a
   frame   x   y
0      0   1   2
1      1   2   2
2      2   2   3
3      3   2   2
4      4   3   2
5      5   4   3
6      6   2   2
7      7  11  10

>>> a.set_index('frame')
        x   y
frame        
0       1   2
1       2   2
2       2   3
3       2   2
4       3   2
5       4   3
6       2   2
7      11  10

>>> type(a._metadata)
list

>>> a._metadata.append(dict())

>>> a.set_index('frame')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-a271477b5f41> in <module>()
----> 1 a.set_index('frame')

/usr/lib/python2.7/dist-packages/pandas/core/frame.pyc in set_index(self, keys, drop, append, inplace, verify_integrity)
   2453             frame = self
   2454         else:
-> 2455             frame = self.copy()
   2456 
   2457         arrays = []

/usr/lib/python2.7/dist-packages/pandas/core/generic.pyc in copy(self, deep)
   2212         """
   2213         data = self._data.copy(deep=deep)
-> 2214         return self._constructor(data).__finalize__(self)
   2215 
   2216     def convert_objects(self, convert_dates=True, convert_numeric=False,

/usr/lib/python2.7/dist-packages/pandas/core/generic.pyc in __finalize__(self, other, method, **kwargs)
   1923         if isinstance(other, NDFrame):
   1924             for name in self._metadata:
-> 1925                 object.__setattr__(self, name, getattr(other, name, None))
   1926         return self
   1927 

TypeError: getattr(): attribute name must be string

附加字典可能不是最终方式,我已将其更改为

>>> a._metadata = dict()

但是,这应该不是,或者是吗?这是一个错误还是我的错误?

谢谢,Kami

0 个答案:

没有答案