How to insert a data frame as an object attribute

Viewed 27

This is most likely a pretty basic question, but I am still learning about classes/objects/constructors/etc. and I am trying to apply some of these concepts to my current workflow.

I am trying to create a class that automatically saves my data frame as a CSV or xlsx file, depending on what I specify, to a given folder. However, I don't believe that I am correctly passing my data frame as an object attribute. This is my code as it stands:

award_date_change = merged_df.loc[merged_df['award_date_change'] == 'yes'] #this is my data frame
class uploading_to_GC:
    
    def __init__(self, file_name, file_type, already_exists): #constructor where I want to pass my data frame, file type to be saved to, and specifying if the file already exists in my folder
        self.file_name = file_name
        self.file_type = file_type
        self.already_exists = already_exists
    def print_file_name(self):
        self.file_name.head(5)
    
    def private_workspace(self):
        commonPath = os.path.expanduser(r"~\path")
        GCdocs = commonPath + '384593683' + '\\' 
        path = GCdocs + "" + file_name
        
        if len(self.file_name) != 0 and self.already_exists == True: #if a file already exists in Gfolder
            if self.file_type == "csv": #for csv files
                    GC_old = pd.read_csv(path)
                    GC_new = GC_old.append(self.file_name, ignore_index=True)
                    GC_new.to_csv(path, index = False)
                    print("csv file is updated to private workspace in GCdocs")
            elif self.file_type == "xlsx": #for xlsx files
                    GC_old = pd.read_csv(path)
                    GC_new = GC_old.append(self.file_name, ignore_index=True)
                    GC_new.to_excel(path, index = False)
                    print("excel file is updated to private workspace in GCdocs")
            else:
                print("unrecognized file type")
                
                
        elif len(self.file_name) != 0 and self.already_exists == False: #if a file does FOLDER already exist in folder
            if self.file_type == "csv": 
                 self.file_name.to_csv(path,index=False)
            if self.file_type == "xlsx": 
                 self.file_name.to_excel(path,index=False)
            else:
                print("unrecognized file type")
                
        else:
            print("there is no data to upload")
        
award_date_change = uploading_to_GC(award_date_change,"csv", False)
award_date_change.private_workspace

I am aware that I don't need to use a class to do this, but I wanted to challenge myself to start using classes more often. Any help would be appreciated

1 Answers

You can pass and store a df in a Class as a data member very simply:

class Foo:
    def __init__(df: pd.DataFrame):
        self.df = df
        # or, if you want to be sure you don't modify the original df
        self.df = df.copy()     


df = pd.DataFrame()
foo_obj = Foo(df)

Edit: the : pd.DataFrame is for type-hinting. This does not affect the actual code, but is merely useful to the reader that we are expecting a pd.DataFrame as input. Good IDEs will also give you an error if you don't pass a DataFrame.

Related