Skip to content

Missing stub for df.map() in pandas-stubs/core/frame.pyi #800

Closed
@andrewpmk

Description

@andrewpmk

In pandas-stubs/core/frame.pyi there is no type stub for df.map(). In pandas 2.1.0 and later, df.applymap() was renamed df.map() https://pandas.pydata.org/docs/dev/reference/api/pandas.DataFrame.map.html and df.applymap() is deprecated.

I am using mypy and I get the following error with this code:
df.map(lambda x: x.strip() if isinstance(x, str) else x)

error: "Series[Any]" not callable [operator]

This code does not generate a mypy error but it is deprecated and generates a deprecation warning in pandas 2.1.0 and later:
df.map(lambda x: x.strip() if isinstance(x, str) else x)

FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.

Here is a test repo demonstrating this bug. https://github.com/andrewpmk/pandas_map_bug I am using pandas 2.1.1, pandas-stubs 2.1.1.230928 and mypy 1.6.1 with Python 3.12.0 on Windows 11.

Output of the test code is as follows:

C:\Users\andre\Documents\git\pandas_map_bug\pandas_map_bug.py:4: FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.
  return df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
applymap
   a
0  a
1  b
2  c
map
   a
0  a
1  b
2  c```

Output of mypy is as follows:
```mypy .
pandas_map_bug.py:7: error: "Series[Any]" not callable  [operator]
Found 1 error in 1 file (checked 1 source file)```

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions