Skip to content

ENH: Broaden dict to Mapping as replace argument #61440

Open
@DavideCanton

Description

@DavideCanton

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions