You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using DataFrame.apply to map rows to a custom type seems to be a valid/supported pattern in pandas (i.e., works at runtime). It looks like the overloads of apply currently do not support this pattern at type checking time.
To Reproduce
Provide a minimal runnable pandas example that is not properly checked by the stubs.
The following examples "maps" a data frame row-wise to a custom type SomeType. Ideally, it would be great if the type checker could infer that list_of_instances is of type list[SomeType] (which it is at runtime).
No overloads for "apply" match the provided argumentsPylancereportGeneralTypeIssues
frame.pyi(1344, 9): Overload 11 is the closest match
Argument of type "(row: Any) -> SomeType" cannot be assigned to parameter "f" of type "(...) -> Series[Any]" in function "apply"
Type "(row: Any) -> SomeType" cannot be assigned to type "(...) -> Series[Any]"
Function return type "SomeType" is incompatible with type "Series[Any]"
"SomeType" is incompatible with "Series[Any]"PylancereportGeneralTypeIssues
Please complete the following information:
OS: [e.g. Windows, Linux, MacOS]: Linux
OS Version [e.g. 22]: Ubuntu 20.04
python version: 3.10.13
version of type checker: mypy 1.11.2
version of installed pandas-stubs: 2.2.2.240909 (latest as of writing)
The text was updated successfully, but these errors were encountered:
bluenote10
changed the title
Row-wise mapping to custom type not supported DataFrame.apply
Row-wise mapping to custom type not supported in DataFrame.applySep 13, 2024
I think we might be able to avoid the map overload issue, but it would probably need to return Series[Unknown/Any] and not Series[SomeType]. Pandas-stubs has made the assumption that Series contains only common types to provide type checking for their operators and some methods.
Describe the bug
Using
DataFrame.apply
to map rows to a custom type seems to be a valid/supported pattern in pandas (i.e., works at runtime). It looks like the overloads ofapply
currently do not support this pattern at type checking time.To Reproduce
The following examples "maps" a data frame row-wise to a custom type
SomeType
. Ideally, it would be great if the type checker could infer thatlist_of_instances
is of typelist[SomeType]
(which it is at runtime).The behavior seems to be the same with mypy and pyright.
mypy
:pyright
:Please complete the following information:
pandas-stubs
: 2.2.2.240909 (latest as of writing)The text was updated successfully, but these errors were encountered: