pandas apply: Expression of type "Series[Unknown]" cannot be assigned to return type "DataFrame"
Environment data
- Language Server version: v2022.7.20 (pyright 7cadfb74)
- OS and version: macOS
- Python version (& distribution if applicable, e.g. Anaconda): python 3.9, pandas 1.4.3
Code Snippet
def normalise(df: DataFrame) -> DataFrame:
return df.apply(lambda x: x)
Repro Steps
Expected behavior
No type error
Actual behavior
Expression of type "Series[Unknown]" cannot be assigned to return type "DataFrame"
"Series[Unknown]" is incompatible with "DataFrame" Pylance[reportGeneralTypeIssues]
Logs
XXX
there are two overloads for that apply function
could convert to dataframe
def normalise(df: DataFrame) -> DataFrame:
return df.apply(lambda x: x).to_frame()
or call the other overload that returns a Dataframe
def normalise(df: DataFrame) -> DataFrame:
return df.apply(lambda x: x, 0 )
Oh interesting, thanks @bschnurr both of those work, and I've learnt more about the pandas type stubs which is great!
However it is non-idiomatic usage of df.apply (at least in our team). Do you know if the type stubs can be improved to address this, or is it impossible to express?
stubs are pulled from here https://github.com/pandas-dev/pandas-stubs
checkout their issues related to apply
and try asking them about usage.
closing since this is related to pandas library itself
better fix. see https://github.com/microsoft/pylance-release/issues/3491#issuecomment-1282691441