Closed
Description
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)