2

I'm trying to create my own class of Dataframe. I would like it has some specific columns when I call it. So I do this:

from pandas import DataFrame

class MyClass(DataFrame):  

    def __init__(self):    
        super(MyClass, self).__init__(columns=['Class','Conditions']) 

However, when I try to append data to it,

test = MyClass()
#test = MyClass(columns=['Class','Conditions'])

test.loc[2] = ['class2',[1, 2, 3]]

I get an error:

File "C:\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 189, in setitem self._setitem_with_indexer(indexer, value)

File "C:\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 451, in _setitem_with_indexer self.obj._data = self.obj.append(value)._data

File "C:\Miniconda3\lib\site-packages\pandas\core\frame.py", line 6211, in append sort=sort)

File "C:\Miniconda3\lib\site-packages\pandas\core\reshape\concat.py", line 226, in concat return op.get_result()

File "C:\Miniconda3\lib\site-packages\pandas\core\reshape\concat.py", line 428, in get_result return (cons._from_axes(new_data, self.new_axes)

File "C:\Miniconda3\lib\site-packages\pandas\core\generic.py", line 356, in _from_axes return cls(data, **kwargs)

TypeError: init() takes 1 positional argument but 2 were given

I realised if I don't declare the columns in __init__ and I assign them after the class is created, I have no problem.

However, I would like to class be created with those names as columns.

Thank very much.

5
  • I can't replicate. Can you provide a minimal reproducible example with something runnable in place of Condition('var2',op.ne,3) ? Commented Nov 23, 2018 at 16:50
  • sorry @jpp my bad. You can replace condition by a list. Like [1,2,3]. I update the example. Thank you. Regards Commented Nov 23, 2018 at 17:50
  • Does it need to be a subclass of DataFrame - unless you're going to be adding/ overriding methods etc... It kind of looks like you could just do MyDataFrame = functools.partial(DataFrame, columns=['Class', 'Condtions']) to fix the column argument for the call and use that? Commented Nov 23, 2018 at 18:01
  • @GermánMartínez, Your code works fine for me in v0.19.2. What version are you using? Commented Nov 23, 2018 at 18:11
  • Hi @jpp the code works, but I want the columns to be predefined when the object is constructed. Regards Commented Nov 27, 2018 at 11:34

1 Answer 1

3

If you want to use the constructor for initialization, you have to pass the arguments to parent __init__

>>> class MyClass(DataFrame):                                                                                                                                              
        def __init__(self, *args, **kwargs):                                                                                                                                   
            kwargs['columns'] = ['Class', 'Conditions']                                                                                                                        
            super(MyClass, self).__init__(*args, **kwargs)                                                                                                                     

>>> md = MyClass({'Class': [1, 2, 3]})
>>> md
   Class Conditions
0      1        NaN
1      2        NaN
2      3        NaN
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.