问题:如何在pycharm上显示pandas.DataFrame, pandas.Series 或numpy.ndarray的类型注解?
解决方案:
- 先使用pip install data-science-types
import pandas, numpy
# 函数foo的参数p1传⼊类型为pandas.DataFrame,参数p2传⼊类型为pandas.Series,
# 参数p3传⼊类型为numpy.ndarray
def foo(p1: pandas.DataFrame, p2: pandas.Series, p3: numpy.ndarray) -> None:
print(type(p1))
print(type(p2))
print(type(p3))
def main():
a = pandas.DataFrame({'qw': [1, 2], 'xs': [3, 4]})
b = pandas.Series([1, 2])
c = numpy.ndarray([1, 3, 2])
d = 1
foo(a, b, c)
# 将光标停留在foo上,pycharm的type hint会提示输入的参数类型有误
foo(a, b, d)
help(foo)
if __name__ == '__main__':
main()
- pycharm 的type hint 提示
- 缺点:
(1)需要额外的依赖包
(2)不能检测出每一列的数据类型