从 pandas 滚动系列中调用任意函数
Calling arbitrary function from pandas rolling series
我可以在 pandas 系列中调用通过字符串变量定义的任意方法。我会这样做的方式是这样的:
import pandas as pd
method_name = 'mean'
pd.Series([1, 2, 3]).__getattr__(method_name)()
现在我想对滚动的 pandas 系列做同样的事情,我会这样做:
import pandas as pd
method_name = 'mean'
pd.Series([1, 2, 3]).rolling(window=1).__getattr__(method_name)()
执行时出现以下错误:
AttributeError: 'Rolling' object has no attribute 'mean'
有没有办法在滚动 pandas 系列上调用任意方法(平均值、中值、最大值、最小值、分位数)?
谢谢!
您可以从 Python 文档(数据模型)中阅读有关 getattr 和 getattribute 的信息:getattr and getattribute
使用getattribute即可解决您的问题;
import pandas as pd
method_name = 'mean'
pd.Series([1, 2, 3]).rolling(window=1).__getattribute__(method_name)()
Output
0 1.0
1 2.0
2 3.0
dtype: float64
必须为每个对象实现方法__getattr__
。这与 __getatrribute__
不同,后者是 object
class 中 Python 中的一种方法。当调用 __getatrr__
时,您将获得此方法的对象实现(如果有)。对于 rolling
,定义如下,它基本上为 caching/pickling 目的(_internal_names_set
)做了一些检查,如果没有找到,它会检查数据帧(self.obj
):
def __getattr__(self, attr: str):
if attr in self._internal_names_set:
return object.__getattribute__(self, attr)
if attr in self.obj:
return self[attr]
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{attr}'"
)
为避免这种情况,请使用 getattr
内置函数:
>> getattr(df.rolling(3), 'mean')
我喜欢 getattr
,因为它允许您在未定义属性时指定第三个可选参数:
>> getattr(df.rolling(3), 'mean', None)
我可以在 pandas 系列中调用通过字符串变量定义的任意方法。我会这样做的方式是这样的:
import pandas as pd
method_name = 'mean'
pd.Series([1, 2, 3]).__getattr__(method_name)()
现在我想对滚动的 pandas 系列做同样的事情,我会这样做:
import pandas as pd
method_name = 'mean'
pd.Series([1, 2, 3]).rolling(window=1).__getattr__(method_name)()
执行时出现以下错误:
AttributeError: 'Rolling' object has no attribute 'mean'
有没有办法在滚动 pandas 系列上调用任意方法(平均值、中值、最大值、最小值、分位数)?
谢谢!
您可以从 Python 文档(数据模型)中阅读有关 getattr 和 getattribute 的信息:getattr and getattribute
使用getattribute即可解决您的问题;
import pandas as pd
method_name = 'mean'
pd.Series([1, 2, 3]).rolling(window=1).__getattribute__(method_name)()
Output
0 1.0
1 2.0
2 3.0
dtype: float64
必须为每个对象实现方法__getattr__
。这与 __getatrribute__
不同,后者是 object
class 中 Python 中的一种方法。当调用 __getatrr__
时,您将获得此方法的对象实现(如果有)。对于 rolling
,定义如下,它基本上为 caching/pickling 目的(_internal_names_set
)做了一些检查,如果没有找到,它会检查数据帧(self.obj
):
def __getattr__(self, attr: str):
if attr in self._internal_names_set:
return object.__getattribute__(self, attr)
if attr in self.obj:
return self[attr]
raise AttributeError(
f"'{type(self).__name__}' object has no attribute '{attr}'"
)
为避免这种情况,请使用 getattr
内置函数:
>> getattr(df.rolling(3), 'mean')
我喜欢 getattr
,因为它允许您在未定义属性时指定第三个可选参数:
>> getattr(df.rolling(3), 'mean', None)