Python 如何将数据帧传递给对象的方法?
为什么这段代码会出现以下错误 TypeError:simple_returns()接受1个位置参数,但给出了2个 该行:Python 如何将数据帧传递给对象的方法?,python,class,dataframe,Python,Class,Dataframe,为什么这段代码会出现以下错误 TypeError:simple_returns()接受1个位置参数,但给出了2个 该行: sim_ret = myRet.simple_returns(c) 调用CalcReturns.simple\u返回(),似乎只传递一个参数。但是python类上的方法是特殊的,因为python也传递对象本身。它在第一个参数中执行此操作。这就是您看到该模式的原因: class MyClass(): def my_method(self): """
sim_ret = myRet.simple_returns(c)
调用CalcReturns.simple\u返回()
,似乎只传递一个参数。但是python类上的方法是特殊的,因为python也传递对象本身。它在第一个参数中执行此操作。这就是您看到该模式的原因:
class MyClass():
def my_method(self):
""" a method with no parameters, but is passed the object itself """
self
被命名为self,作为一种惯例,以提醒我们它是对象。因此,如果要传递数据帧,需要将方法签名更改为:
def simple_returns(self, a_df):
该行:
sim_ret = myRet.simple_returns(c)
调用CalcReturns.simple\u返回()
,似乎只传递一个参数。但是python类上的方法是特殊的,因为python也传递对象本身。它在第一个参数中执行此操作。这就是您看到该模式的原因:
class MyClass():
def my_method(self):
""" a method with no parameters, but is passed the object itself """
self
被命名为self,作为一种惯例,以提醒我们它是对象。因此,如果要传递数据帧,需要将方法签名更改为:
def simple_returns(self, a_df):
只需在类的方法中添加一个参数,即可接收
pandas.Series
,并确保在序列上应用pct\u change()
方法,而不是使用self:
只需在类的方法中添加一个参数,即可接收
pandas.Series
,并确保在序列上应用pct\u change()
方法,而不是使用self:
谢谢那是我的手帕。那真是太棒了