Home > Enterprise >  How do I solve the "AttributeError: 'Series' object has no attribute '_check_fil
How do I solve the "AttributeError: 'Series' object has no attribute '_check_fil

Time:09-15

I am using the following library to fill a Dataframe with modified data (indicator of financial data) https://technical-analysis-library-in-python.readthedocs.io/en/latest/

However, the library has a number of classes that seem to miss certain attributes; or lack the inheritance from another class.

I have created a pandas.Series filled with ones to demonstrate. I call the method aroon_up() from class AroonIndicator with the aforementioned series as input, but I get a 'Series' object has no attribute '_check_fillna'" error. I see that there is no attribute _check_fillna in the AroonIndicator class, but there is in the IndicatorMixin. I have tried to run the Series through the IndicatorMixin class, but it states that this class takes no arguments.

Can someone explain to me what I am doing wrong?

Library

class IndicatorMixin:
"""Util mixin indicator class"""

_fillna = False

def _check_fillna(self, series: pd.Series, value: int = 0) -> pd.Series:
    """Check if fillna flag is True.

    Args:
        series(pandas.Series): calculated indicator series.
        value(int): value to fill gaps; if -1 fill values using 'backfill' mode.

    Returns:
        pandas.Series: New feature generated.
    """
    if self._fillna:
        series_output = series.copy(deep=False)
        series_output = series_output.replace([np.inf, -np.inf], np.nan)
        if isinstance(value, int) and value == -1:
            series = series_output.fillna(method="ffill").fillna(method='bfill')
        else:
            series = series_output.fillna(method="ffill").fillna(value)
    return series

@staticmethod
def _true_range(
    high: pd.Series, low: pd.Series, prev_close: pd.Series
) -> pd.Series:
    tr1 = high - low
    tr2 = (high - prev_close).abs()
    tr3 = (low - prev_close).abs()
    true_range = pd.DataFrame(data={"tr1": tr1, "tr2": tr2, "tr3": tr3}).max(axis=1)
    return true_range

class AroonIndicator(IndicatorMixin):
"""Aroon Indicator

Identify when trends are likely to change direction.

Aroon Up = ((N - Days Since N-day High) / N) x 100
Aroon Down = ((N - Days Since N-day Low) / N) x 100
Aroon Indicator = Aroon Up - Aroon Down

https://www.investopedia.com/terms/a/aroon.asp

Args:
    close(pandas.Series): dataset 'Close' column.
    window(int): n period.
    fillna(bool): if True, fill nan values.
"""

def __init__(self, close: pd.Series, window: int = 25, fillna: bool = False):
    self._close = close
    self._window = window
    self._fillna = fillna
    # self._check_fillna = checkfillna
    self._run()
    self._check_fillna(IndicatorMixin._check_fillna())

def _run(self):
    min_periods = 0 if self._fillna else self._window
    rolling_close = self._close.rolling(
        self._window, min_periods=min_periods)
    self._aroon_up = rolling_close.apply(
        lambda x: float(np.argmax(x)   1) / self._window * 100, raw=True
    )

def aroon_up(self) -> pd.Series:
    """Aroon Up Channel

    Returns:
        pandas.Series: New feature generated.
    """
    aroon_up_series = self._check_fillna(self._aroon_up, value=0)
    return pd.Series(aroon_up_series, name=f"aroon_up_{self._window}")

My program

# Create an empty DataFrame
table = pd.DataFrame()

# Create a serie of ones
list = np.ones((100))
sr = pd.Series(list)
# fill the empty Dataframe with the indicator of the Series
'try 1:'
table['numbers'] = AroonIndicator.aroon_up(sr)

'try 2:'
table['numbers'] = AroonIndicator.aroon_up(IndicatorMixin(sr))

# print the table
print(table)

CodePudding user response:

The Aroon functions return values as panda Series, however you are trying to assign the results to the 'table' variable, which you have initialized as a DataFrame.

Also, when the only parameter you can pass to a function is 'self', you do not include a parameter when you call the function.

Lastly, don't use reserved words like 'list' for variable names.

Try:

import pandas as pd
import numpy as np

list_values = pd.Series(np.ones(100))

sr = AroonIndicator(list_values)

sr = sr.aroon_up()

print(sr)
  • Related