Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将数据帧传递给对象的方法?_Python_Class_Dataframe - Fatal编程技术网

Python 如何将数据帧传递给对象的方法?

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): """

为什么这段代码会出现以下错误

TypeError:simple_returns()接受1个位置参数,但给出了2个

该行:

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:


谢谢那是我的手帕。那真是太棒了