Skip to content

BUG: to_dict(orient='dict') does not convert np.nan to None in Pandas 2.2.3 #61323

Open
@uyauhkk01

Description

@uyauhkk01

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

df = pd.DataFrame({
    'A': [1, np.nan, 3],
    'B': ['x', 'y', np.nan]
})

for k,v in df.to_dict()['A'].items():
    print(f"v={v},type(v)={type(v)}")

print("pd-v:",pd.__version__)
print( df.to_dict()['A'][1] is None) 
print( df.to_dict()['A'][1] is np.nan)
print(df.to_dict()['A'][1])  
print(type(df.to_dict()['A'][1]))  
print(np.isnan(df.to_dict()['A'][1]))

#--------------
# v=1.0,type(v)=<class 'float'>
# v=nan,type(v)=<class 'float'>
# v=3.0,type(v)=<class 'float'>
# pd-v: 2.2.3
# False
# False
# nan
# <class 'float'>
# True

Issue Description

BUG: to_dict(orient='dict') does not convert np.nan to None in Pandas 2.2.3

Expected Behavior

NaN --> None

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO DataIO issues that don't fit into a more specific labelNeeds InfoClarification about behavior needed to assess issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions