Skip to content

BUG: Enabling Copy on Write with DataFrame.replace Raises Exception with np.nan as replacement value #62787

@achapkowski

Description

@achapkowski

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
pd.options.mode.copy_on_write = True
df = pd.DataFrame(data={'A' : [1, 2],'B' : ['b', 'i like pandas'],})
df["Name"] = "I Have a Name"
df['Name2'] = 'i like pandas'
df.replace("i like pandas", "i like arrow")
df.replace(np.nan, None) # works
replace_mappings = {
    pd.NA: None,
    pd.NaT: None,
    np.nan: None # Issue Type
}
df.replace(replace_mappings) # fails

Issue Description

When using DataFrame.replace and trying to replace np.nan value when in a dictionary, it raises an error. A standard replace(np.nan, None) will work though.

The error raised:

builtins.ValueError: <weakref at 0x0000023B896B2840; to 'ExtensionBlock' at 0x0000023B896A26F0> is not in list

Expected Behavior

The expected behavior is that the replace to not error out.

Installed Versions

This is reproducible in the v3 development release as of 10/22/25 and with the current public release. I tested in multiple versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions