【发布时间】:2019-09-03 06:17:29
【问题描述】:
我尝试将 pandas 数据帧的一些基本预处理操作放到一个单独的类中:
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
当我在主脚本中调用此函数时(见下文),我收到错误:
AttributeError: module 'MyClass' has no attribute 'preprocess_dataframe'
这是我的主脚本的相关部分:
import MyClass as mc
path = 'Data.txt'
df = mc.preprocess_dataframe(path)
>>>AttributeError: module 'MyClass' has no attribute 'preprocess_dataframe'
我查了其他几个问题,包括this。然而,尽管我认为修复很容易,但没有任何解决我的问题。感谢您的帮助!
【问题讨论】:
-
我认为你应该这样做
obj = mc.MyClass() -
你需要一个类的实例来调用它的方法或使方法静态
-
@Sparky05 如何让它成为静态的?
-
添加一个@staticmethod 看这里stackoverflow.com/questions/735975/static-methods-in-python
-
我收到错误
'module' object is not callable@SamMason
标签: python pandas function class dataframe