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 · 0 comments
Open
1 of 3 tasks

ENH: Broaden dict to Mapping as replace argument #61440

DavideCanton opened this issue May 14, 2025 · 0 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
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

1 participant