Skip to content

ENH: Broaden dict to Mapping as replace argument #61440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 3 tasks
DavideCanton opened this issue May 14, 2025 · 4 comments
Open
1 of 3 tasks

ENH: Broaden dict to Mapping as replace argument #61440

DavideCanton opened this issue May 14, 2025 · 4 comments
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@DavideCanton
Copy link

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Currently the replace method of Series allows only dict, but not Mapping inputs, as the DataFrame one does.

For example:

from collections.abc import Mapping
import pandas as pd


df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})

d: Mapping[int, str] = {1: "a", 2: "b", 3: "c"}

d2: Mapping[str, Mapping[int, str]] = {"A": d}
print(df.replace(d2))  # typechecks

print(df["A"].replace(d))  # works but doesn't typecheck

Feature Description

I guess it's enough to change from dict to Mapping in the type signature, since it seems to work even if the argument is not a dict (for example if it's a MappingProxyType instance).

Alternative Solutions

I guess an alternative solution is just to type ignore the replace invocation.

Additional Context

No response

@DavideCanton DavideCanton added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels May 14, 2025
@loicdiridollou
Copy link
Member

loicdiridollou commented May 20, 2025

Hey @DavideCanton,

Have you installed the pandas-stubs package? It is an extension of the pandas repo for types.
I have tried locally in an environment where pandas-stubs is installed and no warning are raised (pyright/mypy).
Can you provide how you typecheck this code?

See below, the type is Mapping as you are suggesting:
https://github.com/pandas-dev/pandas-stubs/blob/93a9e11f4e4656a935853d3814ac9073adf4c9cc/pandas-stubs/core/frame.pyi#L863-L880

@DavideCanton
Copy link
Author

This is what I do

$ uv pip install pandas pandas-stubs mypy
Resolved 11 packages in 17ms
Installed 11 packages in 4.57s
 + mypy==1.15.0
 + mypy-extensions==1.1.0
 + numpy==2.2.6
 + pandas==2.2.3
 + pandas-stubs==2.2.3.250308
 + python-dateutil==2.9.0.post0
 + pytz==2025.2
 + six==1.17.0
 + types-pytz==2025.2.0.20250516
 + typing-extensions==4.13.2
 + tzdata==2025.2
$ mypy .
foo.py:10: error: Argument 1 to "replace" of "DataFrame" has incompatible type "Mapping[str, Mapping[int, str]]"; expected "str | bytes | date | datetime | timedelta | <14 more items> | None"  [arg-type]
Found 2 errors in 2 files (checked 2 source files)

@loicdiridollou
Copy link
Member

I see why you are seeing the issue and I don't, can you try running with pandas-stubs on main:
uv pip install "git+https://github.com/pandas-dev/pandas-stubs.git"
With your setup I saw the error, with the stubs repo on main I don't meaning that it was fixed in: pandas-dev/pandas-stubs#1164
Please let me know if when pulling from main you still see the issue, we should be able to ask for a new release of the stubs.

@DavideCanton
Copy link
Author

Seems fixed if using the main version, so probably as you said it's just a stubs problem.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants