我已经使用numpy/pandas多年了,但最近才开始使用mypy对所有代码进行类型检查。
以下示例显示了按预期工作但未通过mypy的代码。在这个例子中添加类型注释的正确方法是什么?
import pandas as pd
import numpy as np
def get_repeats(date: pd.Timestamp)->np.ndarray:
return np.repeat(date,3)
if __name__ == '__main__':
today=pd.Timestamp.utcnow()
repeats=get_repeats(today)
print(repeats)
编辑:mypy错误为
minimal_example.py:5: error: Argument 1 to "repeat" has incompatible type "Timestamp"; expected "Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]"
查看文档,从技术上讲,np.repeat
需要一个类似数组的第一个参数,而时间戳不是。
然而,列表中的时间戳是。换句话说,
def get_repeats(date: pd.Timestamp) -> np.ndarray:
return np.repeat([date], 3)
让mypy开心:
$ mypy zup.py
Success: no issues found in 1 source file
$ cat zup.py
import numpy as np
import pandas as pd
def get_repeats(date: pd.Timestamp) -> np.ndarray:
return np.repeat([date], 3)
if __name__ == '__main__':
today = pd.Timestamp.utcnow()
repeats = get_repeats(today)
print(repeats)