无法使用`loc`将行添加到`pandas.DataFrame`子类中

时间:2019-07-05 13:54:37

标签: python pandas dataframe

只需将pandas.DataFrame子类化并像这样包装loc

import pandas.DataFrame

class XXX(pandas.DataFrame):

    def __init__(self):
        super(Override, self).__init__(columns=['operation'])

    def add_row(self, index, value):
        self.loc[index] = value

这是添加行时得到的:

============================= ERRORS =============================
Traceback (most recent call last):
  File "/home/samuel/git/xxx/tests/test_xxx.py", line 21, in setUp
    self.override.add_override(ticker, override)
  File "/home/samuel/git/xxx/xxx.py", line 198, in add_override
    self.loc[ticker] = override
  File "/home/samuel/.local/lib/python2.7/site-packages/pandas/core/indexing.py", line 190, in __setitem__
    self._setitem_with_indexer(indexer, value)
  File "/home/samuel/.local/lib/python2.7/site-packages/pandas/core/indexing.py", line 451, in _setitem_with_indexer
    self.obj._data = self.obj.append(value)._data
  File "/home/samuel/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 6692, in append
    sort=sort)
  File "/home/samuel/.local/lib/python2.7/site-packages/pandas/core/reshape/concat.py", line 229, in concat
    return op.get_result()
  File "/home/samuel/.local/lib/python2.7/site-packages/pandas/core/reshape/concat.py", line 431, in get_result
    return (cons._from_axes(new_data, self.new_axes)
  File "/home/samuel/.local/lib/python2.7/site-packages/pandas/core/generic.py", line 341, in _from_axes
    return cls(data, **kwargs)
TypeError: __init__() takes exactly 1 argument (2 given)

0 个答案:

没有答案