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
124k29 gold badges181 silver badges319 bronze badges
asked Apr 12, 2019 at 13:10
7
  • I think you should be doing obj = mc.MyClass() Commented Apr 12, 2019 at 13:13
  • you need an instance of the class to call its method or make the method static Commented Apr 12, 2019 at 13:15
  • @Sparky05 How do I make it static? Commented Apr 12, 2019 at 13:22
  • Add an @staticmethod see here stackoverflow.com/questions/735975/static-methods-in-python Commented Apr 12, 2019 at 13:23
  • I receive the error 'module' object is not callable @SamMason Commented Apr 12, 2019 at 13:34

2 Answers 2

3

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.

answered Apr 12, 2019 at 13:30
Sign up to request clarification or add additional context in comments.

7 Comments

I receive the error 'module' object is not callable
I expected this error. You will have change the import statement as well to : ` from MyClassfilename import MyClass as mc`
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?
Did you add mc().preprocess_dataframe(path) as suggested in the answer? Emphasis on mc()
Yes I included it
|
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)
answered Apr 12, 2019 at 13:39

5 Comments

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?
You must remove the self argument. I have changed the method, too. (Removing all references to self)
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!
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
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. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.