Home > Net >  Python dataframe class __init__ weird behaviour
Python dataframe class __init__ weird behaviour

Time:01-26

So I'm trying to restructure files in my project and created a following class

File: merge.py

class DataFrame:
def __init__(self):
    self.df = pd.DataFrame()

in other file I'm calling it

File:merge_files.py

from merge import Dataframe
dfx = DataFrame()

here's the output for:

print(dfx)

OUTPUT:

<merge.DataFrame object at 0x81503d790>

and if I print following:

print(dfx.df)

OUTPUT:

Empty DataFrame
Columns: []
Index: []

I searched for hours and found no answer. Can someone explain this and propose how to get to empty dataframe using just dfx instead of dfx.df

Many thanks

CodePudding user response:

What your code currently does is create a generic object that has the field df that stores a pandas DataFrame. So the empty datafrrame is a field of the object, but not the object itself.

Instead, you want to inherit the pandas DataFrame class so that when you instantiate your DataFrame class, it's also a pandas DataFrame.

import pandas as pd
class MyDataFrame(pd.DataFrame):
    def __init__(self):
        super().__init__()
        
print(MyDataFrame())

CodePudding user response:

I end up modifying Michael's code so one method performs two tasks, see code below

class NewDataFrame(pd.DataFrame):
def __init__(self,df: pd.DataFrame):
    super().__init__(df)

Example of call:

1. df = NewDataFrame(None)
2. df = NewDataFrame(pd.DataFrame(columns=constant.COLUMNS_REPORT))
3. df = NewDataFrame(df_something)

Thank you all for your help

  • Related