解决technical-analysis-library-in-python中'Series'无'_check_fillna'属性错误
错误分析与修正方案
1. 核心操作错误:混淆类方法与实例方法
你直接用AroonIndicator.aroon_up(sr)调用方法,这是把实例方法当成了类方法调用。aroon_up是AroonIndicator的实例方法,必须先创建类的实例,再通过实例调用方法,不能直接用类名调用。
2. 错误尝试实例化Mixin类
IndicatorMixin是一个工具混合类,设计目的是让其他类继承它来获取功能,本身不需要也不能直接实例化(它没有定义接受参数的构造方法),所以IndicatorMixin(sr)这种写法完全错误。
3. 库代码本身存在bug
你提供的AroonIndicator类的__init__方法最后一行self._check_fillna(IndicatorMixin._check_fillna())是严重错误的:
_check_fillna是实例方法,调用时不需要手动传self,且参数应该是Series和填充值,这里的调用方式完全不符合规则- 这行代码会导致初始化实例时直接报错
正确解决步骤
第一步:修复库中AroonIndicator的构造方法
删掉__init__里的错误代码,正确的构造方法如下:
def __init__(self, close: pd.Series, window: int = 25, fillna: bool = False): self._close = close self._window = window self._fillna = fillna self._run()
第二步:修改测试代码,正确创建实例并调用方法
import pandas as pd import numpy as np # 先放入修复后的IndicatorMixin和AroonIndicator类代码 # 创建空DataFrame table = pd.DataFrame() # 生成全1的Series sr = pd.Series(np.ones(100)) # 正确流程:先创建AroonIndicator实例 aroon_instance = AroonIndicator(sr, window=25, fillna=False) # 再调用实例的aroon_up方法 table['numbers'] = aroon_instance.aroon_up() # 输出结果 print(table)
关键知识点
- 混合类(Mixin)是用来给子类注入功能的,不能直接实例化,子类继承后会自动拥有Mixin中的方法和属性
- 实例方法必须通过类的实例调用,类名直接调用仅适用于被
@classmethod或@staticmethod装饰的方法 AroonIndicator继承IndicatorMixin后,实例可以直接调用self._check_fillna(),不需要额外处理继承逻辑
内容的提问来源于stack exchange,提问作者Bende
相关产品推荐
相关产品推荐

