1

I have two functions that take in a pd.Series argument

def function_1(ages:pd.Series)->float:
    ...

and

def function_2(names:pd.Series)->int:
    ...

However, for function_1 the input is a pd.Series of integers, and for function_2 it's a pd.Series of strings.

I'd like to be able to write:

def function_1(ages:pd.Series[int])->float:
    ...

def function_2(names:pd.Series[str])->int:
    ...

However this gives the error:

TypeError: 'type' object is not subscriptable

Note: for now I'm doing

def function_1(ages:'pd.Series[int]')->float:
    ...

def function_2(names:'pd.Series[str]')->int:
    ...
MYK
  • 1,988
  • 7
  • 30
  • 2
    Does this answer your question? [How to specify the type of pandas series elements in type hints?](https://stackoverflow.com/questions/57854936/how-to-specify-the-type-of-pandas-series-elements-in-type-hints) – Hamza usman ghani Feb 18 '22 at 14:38

1 Answers1

-1

Your example should work in Python >= 3.9.

To get it to work in Python 3.8 (and possibly earlier 3.x's)

from __future__ import annotations

The PEPs around typing continue to evolve, as does Python...

dsz
  • 4,542
  • 39
  • 35