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 returnpd.Series([0, 1])
). I did this:
def f() -> pd.Series[str]:
But
TypeError: 'type' object is not subscriptable
So, how to specify the type of pandas series elements in type hints?. Any ideas?
pd.Series(dtype=str)
allows you to specify the data type of a series' elements. My guess is that this also works for type hints. – Fathomlesspd.Series(dtype=str)
does not work for type hints. – Aeonianpd.Series(dtype=str)
does not work for type hints? My 3.7 interpretor at least accepts it syntactically. – Aqaba