1

I am interested to prepare a class object which can inherit the attributes from another class. However, the initialization should be done by using an existing method from another class. One can try:

import pandas as pd

class Data():
    def __init__(self, file):
        self.df = pd.read_csv(file)

if __name__ = "__main__":
    abc = Data("xyz.csv")

In this manner, the instance 'abc.df' will be a DataFrame instance which is initialized by using pandas.read_csv() method, reading the "xyz.csv" file. I wonder how one can implement the initialization such that the 'abc' itself will be the DataFrame instance instead of 'abc.df'? It shall be something like

import pandas as pd

class Data():
    def __init__(self, file):
        self = pd.read_csv(file) # self has all the attributes of a DataFrame instance

if __name__ = "__main__":
    abc = Data("xyz.csv")

Of course, this won't work.

Edit: I found a relevant discussion on this How to subclass Pandas.DataFrame. However, I am still curious whether there is a way to create an instance by using the existing method of another instance.

4
  • def data(file): return pd.read_csv(file)...!? Commented Jun 15, 2015 at 20:06
  • 1
    Why do you need to write such a class at all, instead of just calling read_csv and using the result? Commented Jun 15, 2015 at 20:12
  • deceze, BrenBarn: The purpose of doing so is not restricted to pandas only. My intention is to initialize an instance which has all the attributes and methods from the existing instance. On top of that I could add on additional attributes and methods for the other purposes if needed. Commented Jun 16, 2015 at 5:54
  • deceze: How does declaring a separate function can help to initialize an instance that meets the requirement as mentioned? Commented Jun 16, 2015 at 19:11

1 Answer 1

0

You could subclass Data from pandas.DataFrame, and in the constructor use from_csv to read the csv data (not verified):

import pandas as pd

class Data(pd.DataFrame):
    def __init__(self, file):
         super(Data, self).__init__()
         self.from_csv(file)

if __name__ = "__main__":
abc = Data("xyz.csv")
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the suggestion. I may have done it the wrong way when I tried to subclass 'Data' from 'pandas.DataFrame'
I tried to use super().from_csv(file), within the __init__(self, file). The programme does not complain if I create an instance by abc = Data("xyz.csv") However, it will give a problem RuntimeError: maximum recursion depth exceeded when I try to print abc.shape Furthermore, from_csv has a slightly different behaviour as compared to read_csv. Refer to this <a href="stackoverflow.com/questions/26495408/…>.
Unfortunately, the updated answer does not work such that we can't access the attributes such as print(abc.shape), or print(abc.columns) in this manner.
Updated, see also stackoverflow.com/questions/13229750/… . The thing might not be supported: github.com/pydata/pandas/issues/2485
Thank you for your suggestion. As mentioned, for Pandas does not support this kind of subclassing, and we can't access the attributes of a DataFrame by following the suggested answer. Perhaps it is not possible for me to initialize an instance object by using an existing method of another instance in Python.

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.