考虑以下代码:
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
在包含此代码的文件上从mac终端命令行运行mypy大约需要40秒。这正常吗?发现以下错误:
xxx.py:8: error: "Series[Any]" has no attribute "abs"
Found 1 error in 1 file (checked 1 source file)
我经常收到抱怨熊猫系列方法的错误。这是怎么回事?代码肯定会运行并产生预期的结果。
据我所知,到目前为止,panda还没有打字存根。
例如,当我尝试在下面的脚本上运行mypy
时,会出现以下错误。
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)
我想您已经安装了data-science-types
包,它为panda提供了类型存根。
安装后,我确实遇到了同样的错误。
pandas_typing.py:5: error: "Series[Any]" has no attribute "abs"
Found 1 error in 1 file (checked 1 source file)
我仔细查看了代码,注意到Series
实际上没有方法abs
:https://github.com/predictive-analytics-lab/data-science-types/blob/master/pandas-stubs/core/series.pyi
您可能想在此处提交一个缺少打字存根的问题。https://github.com/predictive-analytics-lab/data-science-types/issues