pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

pandas apply: Expression of type "Series[Unknown]" cannot be assigned to return type "DataFrame"

Open tekumara opened this issue 3 years ago • 2 comments

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

tekumara avatar Jul 12 '22 07:07 tekumara

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 )

bschnurr avatar Jul 12 '22 17:07 bschnurr

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?

tekumara avatar Jul 13 '22 00:07 tekumara

stubs are pulled from here https://github.com/pandas-dev/pandas-stubs

checkout their issues related to apply

and try asking them about usage.

bschnurr avatar Oct 13 '22 18:10 bschnurr

closing since this is related to pandas library itself

bschnurr avatar Oct 13 '22 23:10 bschnurr

better fix. see https://github.com/microsoft/pylance-release/issues/3491#issuecomment-1282691441

bschnurr avatar Oct 18 '22 16:10 bschnurr