1

Consider the following code:

import pandas as pd
  

def calc_mard(x: pd.Series, y: pd.Series) -> float:

    x_at_y_timestamps: pd.Series = x[y.index]
    error: pd.Series = y - x_at_y_timestamps
    mard: float = 100.0 * (error.abs() / x).mean()

    return mard

Running mypy from a mac terminal command line on a file containing this code takes around 40 seconds. Is this normal? The following error is found:

xxx.py:8: error: "Series[Any]" has no attribute "abs"
Found 1 error in 1 file (checked 1 source file)

I frequently get errors complaining about pandas series methods. What's going on here? The code certainly runs and produces expected results.

rhz
  • 960
  • 14
  • 29

1 Answers1

2

To the best of my knowledge, as of now, pandas does not have typing stubs.

For example, I get the following error when trying to run mypy on a script below.

import pandas as pd


def compute(ser1: pd.Series, ser2: pd.Series) -> float:
    return 100.0 * (ser1.abs() / ser2).mean()


if __name__ == '__main__':
    ser1: pd.Series = pd.Series([1, 2, 3])
    ser2: pd.Series = pd.Series([3, 4, 5])
    result = compute(ser1, ser2)
    print(result)
pandas_typing.py:1: error: Skipping analyzing 'pandas': found module but no type hints or library stubs
pandas_typing.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

I suppose that you have data-science-types package installed, which provides type stubs for pandas.

With that installed I indeed get the same error.

pandas_typing.py:5: error: "Series[Any]" has no attribute "abs"
Found 1 error in 1 file (checked 1 source file)

I looked through the code and noticed that indeed there is no method abs for Series: https://github.com/predictive-analytics-lab/data-science-types/blob/master/pandas-stubs/core/series.pyi

You may want to submit the issue here on a missing typing stub. https://github.com/predictive-analytics-lab/data-science-types/issues

Maxim Ivanov
  • 299
  • 1
  • 6
  • Indeed, I have both ```data-science-types``` and ```pandas-stubs``` installed. There are a slew of errors--from problems with ```pd.Interval``` and associated ```overlap``` method, the ```name``` attribute of ```my_series.index``` being unrecognized, and many other operations on indices being unrecognized. I think that I probably just over-estimated the maturity of the stub support from these two packages. – rhz May 04 '21 at 21:51