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!
2 Answers 2
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.
7 Comments
'module' object is not callableTypeError: 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?mc()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)
5 Comments
TypeError: preprocess_dataframe() missing 1 required positional argument: 'path' Maybe it has to do something with having (self, path as arguments before?
obj = mc.MyClass()'module' object is not callable@SamMason