Home > OS >  Class that returns a transformed dataframe
Class that returns a transformed dataframe

Time:09-30

I'm trying to create a class that takes the path and name of the CSV file, converts it to a dataframe, deletes some columns, converts another one to datetime, as in the code

import os
from pathlib import Path
import pandas as pd
import datetime

class Plans:
    def __init__(self, file , path):
        self.file = file
        self.path = path
        self.df = pd.Dataframe()
        
    def get_dataframe(self):
        os.chdir(self.path)
        self.df = pd.read_csv(self.file, encoding="latin-1", low_memory=False, sep=';')
        if 'data' in df.columns:
            self.tipo = 'sales'
            self.df['data'] = pd.to_datetime(df['data'])
        return clean_unused_data()
    
    def clean_unused_data(self):        
        columns = ['id', 'docs', 'sequence','data_in','received', 'banc', 'return', 'status',  'return_cod', 
                   'bank_account_return',  'id_transcript', 'id_tx','type_order']
        for item in columns:
            del self.df[item]
        del columns[:]        
        return self.df

When I call an object of the class it gives an error with the clean_unused_data function returns the following error:

__getattr__ raise AttributeError(f"module 'pandas' has no attribute '{name}'")

Also, I would like to do more dataframe transformations in the Plans class. but since this first one failed, I was a little lost.

Thanks for the help and I apologize for the lack of intimacy with python

CodePudding user response:

I think the error refers to calling an attribute that does not exist in Pandas. From what I can see you wrote pd.DataFrame as pd.Dataframe. Notice the capitalization.

Try the following:

def __init__(self, file , path):
    self.file = file
    self.path = path
    self.df = pd.DataFrame()

CodePudding user response:

Probably one of the columns you are trying to delete is not actually in your file. You can handle the exception or remove this column label from your array.

  • Related