pandas.Dataframe的列名称和类型是使用mypy还是其他静态方式?

问题描述

我有一个函数func,它接受​​一个pandas.Dataframe,该数据框必须具有列:'col_foo','col_bar',并且它们的类型必须为pd.Timestamp,np.integer才能使func没有失败。

我如何确保这些列以静态方式存在?


我可以做这样的事情:

def func(df: pd.Dataframe):
    cols_struct = {'col_foo': pd.Timestamp,'col_bar': np.integer}
    # also check the columns types here
    if not set(df.columns).issuperset(cols_struct.keys()):
        raise ValueError('the dataframe must be structured like',cols_struct)
    

或者使用现有的库,并与装饰器巧妙地结合在一起。

但是我希望我的代码在mypy静态类型检查期间而不是在运行时失败,我该如何键入提示func

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)