How to specify the type of pandas series elements in type hints?

非 Y 不嫁゛ 提交于 2020-05-12 20:32:36

问题


My function returns a pandas series, where all elements have a specific type (say str). The following MWE should give an impression:

import pandas as pd 
def f() -> pd.Series:
    return pd.Series(['a', 'b']) 

Within the type hints I want to make clear, that f()[0] will always be of type str (compared for example to a function that would return pd.Series([0, 1])). My initial guess was to use def f() -> pd.Series[str]: what gives the TypeError: 'type' object is not subscriptable.

So, how to specify the type of pandas series elements in type hints?


回答1:


Unfortunately Python's type hinting does not support this out of the shelf. Nonetheless, you can always make use of dataenforce library (link) to add hints or even enforce validation.



来源:https://stackoverflow.com/questions/57854936/how-to-specify-the-type-of-pandas-series-elements-in-type-hints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!