Skip to content

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

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
uyauhkk01 opened this issue Apr 21, 2025 · 1 comment
Open
1 of 3 tasks
Labels
Bug IO Data IO issues that don't fit into a more specific label Needs Info Clarification about behavior needed to assess issue

Comments

@uyauhkk01
Copy link

uyauhkk01 commented Apr 21, 2025

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()

@uyauhkk01 uyauhkk01 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 21, 2025
@rhshadrach rhshadrach added Needs Info Clarification about behavior needed to assess issue IO Data IO issues that don't fit into a more specific label and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 24, 2025
@rhshadrach
Copy link
Member

Thanks for the report. Why would you expect pandas to convert np.nan to None here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Data IO issues that don't fit into a more specific label Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

2 participants