Skip to content

Series.apply() should accept None as Callable return type #557

Closed
@chetanpepper

Description

@chetanpepper

Describe the bug
Pandas is fine if the Callable passed to series.apply(...) returns None, but mypy throws an error. Passing a null-returning Callable to df.apply() does not throw an error.

To Reproduce

import pandas as pd

s = pd.Series([{'a': 1, 'b':2}])
s['c'] = s.apply(lambda x: None)

mypy fails with

a.py:4: error: Argument 1 to "apply" of "Series" has incompatible type "Callable[[Any], None]"; expected "Callable[..., Union[str, bytes, date, datetime, timedelta, datetime64, timedelta64, bool, int, float, Timestamp, Timedelta, complex, Sequence[Any], Mapping[Any, Any]]]"  [arg-type]
a.py:4: error: Incompatible return value type (got "None", expected "Union[str, bytes, date, datetime, timedelta, datetime64, timedelta64, bool, int, float, Timestamp, Timedelta, complex, Sequence[Any], Mapping[Any, Any]]")  [return-value]
Found 2 errors in 1 file (checked 1 source file)

Please complete the following information:

  • MacOS
  • 12.6
  • python3.10
  • mypy 1.0.1
  • pandas-stubs 1.5.3.230227

Additional context
Using a data frame instead of a series results in no error:

import pandas as pd

df = pd.DataFrame([{'a': 1, 'b':2}])
df['c'] = df.apply(lambda x: None)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions