Search code examples
pythonpandasmypypython-typing

Pandas 'type' object is not subscriptable


I am trying to type a a function which receives a Series

from typing import Any
from pandas import Series


def func(w: Series[Any], v: Series[Any]) -> int:

However I got the error

TypeError: 'type' object is not subscriptable

What I am doing wrong?


Solution

  • Adding quotes around the type will fix this issue.

    In the code you provided:

    from typing import Any
    from pandas import Series
    
    
    def func(w: 'Series[Any]', v: 'Series[Any]') -> int:
        # your code
        pass
    

    Modern editors will identify the type and raise appropriate warnings or errors if violated.

    You are not doing anything "wrong", since this won't have raised any warnings or error for builtin types i.e. list, dict, set, etc. However, using quotes is the safer choice since it will work in all these cases and yours as well.