Skip to content

Conversation

@max-sixty
Copy link
Collaborator

Fixes #10833

Problem

equivalent() returned False for NumPy scalar NaN comparisons like np.float64(np.nan), causing NaN attributes to be incorrectly dropped when using combine_attrs="drop_conflicts" after NetCDF roundtrip.

Root Cause

np.float64(np.nan) == np.float64(np.nan) returns np.bool_(False) (not Python bool). The old code converted this to Python bool and returned False immediately, preventing the NaN equivalence check from running.

Fix

Check for NaN equivalence before the equality comparison, avoiding the np.bool_ type issue entirely.

Changes

  • xarray/core/utils.py: Moved NaN check before equality comparison
  • xarray/tests/test_utils.py: Added tests for Python float & NumPy scalar NaN
  • xarray/tests/test_concat.py: Added integration test for NetCDF roundtrip scenario

🤖 Generated with Claude Code

max-sixty and others added 2 commits October 9, 2025 15:03
Fixes pydata#10833 where NaN attributes were incorrectly dropped after NetCDF
roundtrip when using combine_attrs="drop_conflicts".

The issue: np.float64(np.nan) == np.float64(np.nan) returns np.bool_(False)
instead of Python bool, causing the code to return False before reaching
the NaN equivalence check.

The fix: Check for NaN equivalence before doing the equality comparison,
avoiding the np.bool_ type trap entirely.

Co-authored-by: Claude <[email protected]>
The test requires NetCDF backend libraries (netCDF4, h5netcdf, or scipy)
which are not available in the bare-minimum test environment.

Co-authored-by: Claude <[email protected]>
@max-sixty max-sixty merged commit 3a05647 into pydata:main Oct 10, 2025
37 checks passed
@max-sixty max-sixty deleted the scalar-nan-types branch October 10, 2025 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Saving and reloading DataArray from NetCDF breaks comparison of NaN attributes

2 participants