Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 999
Rodrigo Avatar asked Aug 21 '26 20:08

Rodrigo


1 Answers

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.

like image 126
DaniyalAhmadSE Avatar answered Aug 24 '26 08:08

DaniyalAhmadSE