2

I tried to put some basic preprocessing operations of a pandas dataframe into a seperate class:

import pandas as pd
import numpy as np
from numba import jit

class MyClass:
    def _init_(self):
        pass

    @jit

    def preprocess_dataframe(self, path):
        self.df = pd.read_csv(path, index_col=False, delimiter=' ' , names=['Time', 'Downloads', 'ServerID', 'Server', 'Date'], usecols=['Time', 'Downloads', 'Server', 'Date'])

        print(self.df.head(5))
        self.df['Date'] = self.df['Date'].astype(str)
        self.df['Timestamp'] = pd.to_datetime(self.df['Time'] +' '+ self.df['Date'], format='%H:%M:%S %Y%m%d')
        self.df[['Server_alone', 'Instance']] = self.df['Server'].str.split('-' ,expand=True)

        self.df.drop(columns=['Time'], inplace=True)

        self.df['Date'] = pd.to_datetime(self.df['Date'], format='%Y-%m-%d')

        self.df.set_index(self.df['Date'])

        return self.df

When I call this function in my main script (see below) I receive the error:

AttributeError: module 'MyClass' has no attribute 'preprocess_dataframe'

This is the relevant part of my main script:

import MyClass as mc

path = 'Data.txt'

df = mc.preprocess_dataframe(path)

>>>AttributeError: module 'MyClass' has no attribute 'preprocess_dataframe'

I looked up several other questions including this. However, nothing solved my issue despite I think that the fix is quite easy. Thank you for your help!

martineau
  • 119,623
  • 25
  • 170
  • 301
Mike_H
  • 1,343
  • 1
  • 14
  • 31

2 Answers2

4

You haven't created an instance of the MyClass. You could rectify it by:

df = mc().preprocess_dataframe(path)

Also change the import statement as well to : from filename import MyClass as mc

You could also make preprocess_dataframe a staticmethod as mentioned in comments.

GGJON
  • 325
  • 1
  • 13
  • I receive the error ```'module' object is not callable``` – Mike_H Apr 12 '19 at 13:32
  • 1
    I expected this error. You will have change the import statement as well to : ` from MyClassfilename import MyClass as mc` – GGJON Apr 12 '19 at 13:36
  • I think we are close, now I get this error while calling the function ```TypeError: preprocess_dataframe() missing 1 required positional argument: 'path'```. It must be connected to the method defintion. Do I have to provide something for self as well? – Mike_H Apr 12 '19 at 13:44
  • Did you add mc().preprocess_dataframe(path) as suggested in the answer? Emphasis on `mc()` – GGJON Apr 12 '19 at 13:50
  • Yes I included it – Mike_H Apr 12 '19 at 13:51
  • That's strange. It seems to run fine here. – GGJON Apr 12 '19 at 14:00
  • It perfectly works now. I named MyClass differently in my code. It was kind of an example code right here. Failed to replace everything properly on copy paste. Thanks a lot! – Mike_H Apr 12 '19 at 14:13
1

You should make the method static

import pandas as pd
import numpy as np
from numba import jit

class MyClass:
    @jit
    @staticmethod
    def preprocess_dataframe(path):
        df = pd.read_csv(path, index_col=False, delimiter=' ' , names=['Time', 'Downloads', 'ServerID', 'Server', 'Date'], usecols=['Time', 'Downloads', 'Server', 'Date'])

        print(self.df.head(5))
        df['Date'] = df['Date'].astype(str)
        df['Timestamp'] = pd.to_datetime(df['Time'] +' '+ df['Date'], format='%H:%M:%S %Y%m%d')
        df[['Server_alone', 'Instance']] = df['Server'].str.split('-' ,expand=True)

        df.drop(columns=['Time'], inplace=True)

        sdf['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')

        df.set_index(df['Date'])

        return df

and call it the following way

from filename import MyClass

path = 'Data.txt'

df = MyClass.preprocess_dataframe(path)
Sparky05
  • 4,692
  • 1
  • 10
  • 27
  • That answer might be pretty close. However, I receive this error now: ```TypeError: preprocess_dataframe() missing 1 required positional argument: 'path'``` Maybe it has to do something with having ```(self, path``` as arguments before? – Mike_H Apr 12 '19 at 13:48
  • You must remove the self argument. I have changed the method, too. (Removing all references to self) – Sparky05 Apr 12 '19 at 14:05
  • It perfectly works now. I named MyClass differently in my code. It was kind of an example code right here. Failed to replace everything properly on copy paste. Thanks a lot! – Mike_H Apr 12 '19 at 14:13
  • If you don't want to bundle multiple function inside this class, you don't need the class and could simply define and call the function – Sparky05 Apr 12 '19 at 15:07
  • As I have multiple other functions defined in my class, I will keep it in this structure. But thank you for this input, which might ge relevant for other users having the same question. :) – Mike_H Apr 15 '19 at 11:17