Home > Software design >  How can I assign a pandas dataframe to a class variable?
How can I assign a pandas dataframe to a class variable?

Time:09-22

I have different methods in my class which are using the same pandas dataframe. Instead of passing the same dataframe as an argument to each method, is there a way I can declare the dataframe as a class variable so that all the methods can share it.

I tried the solution given here but couldn't make it work. Assign a pandas dataframe to an object as a static class variable - memory use (Python)

An example of what I am trying to do is

import pandas as pd
df_temp = pd.DataFrame()
df_temp = some_df.copy()     #Assume that I am copying some_df to df_temp

class Weather:
  # I tried using the below and not pass the dataframe to my methods but it didnt work.
  # df = df_temp
  def __init__(self, baseyear):
    self.baseyear = baseyear
   
  def HU_monthly(self, df, month):
    df_HU = df.groupby(['Station','Year','Month'])['Heat Units'].sum().round(2).reset_index()
    return(df_HU)
  
  def HU_range(self, df, first_month, last_month):
    df_between_months = df[(first_month <=df['Month'])&(df['Month']<=last_month)]
    return(df_between_months)

monthly = Weather(2000)
df_1 = monthly.HU_monthly(df_temp, 8)

ranger = Weather(2010)
df_2 = ranger.HU_range(df_temp, 5, 10)

The dataframe(df_temp) I am passing as an argument is same for both the cases, what is the best way of eliminating the need to pass it?

CodePudding user response:

You can pass your dataframe while constructing the object and assign it into an instance variable like this:

class Weather:
    def __init__(self, df):
        self.df = df

Then you can access the dataframe in all your methods like this:

def HU_monthly(self, month):
    df_HU = self.df.groupby(['Station','Year','Month'])['Heat Units'].sum().round(2).reset_index()
    return(df_HU)

Create your class object as following:

weather = Weather(df)
  • Related