Skip to content

ENH: DataFrame.replace does not work with dictionary subclasses #34257

Open
@timgeb

Description

@timgeb

Is your feature request related to a problem?

I want to use DataFrame.replace with a dictionary that has a custom __getitem__ logic. The current behavior is easy to demonstrate with a defaultdict (see code below).

Describe the solution you'd like

I would like DataFrame.replace to honor the __getitem__ method of dict-like arguments.

API breaking implications

I don't know.

Code

>>> from collections import defaultdict
>>> import pandas as pd
>>> df = pd.DataFrame([1, 2, 3])
>>> d = defaultdict(int, {1: -1, 2: -2})
>>> df.replace(d)
   0
0 -1
1 -2
2  3
>>> d[3]
0

Expected behavior: the 3 inside the DataFrame should be replaced with 0, because d[3] returns 0.

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