Skip to content

Fix padding an integer array with non-finite constant_values (NaN)#11432

Open
PranavAchar01 wants to merge 2 commits into
pydata:mainfrom
PranavAchar01:fix/6431-pad-integer-nan
Open

Fix padding an integer array with non-finite constant_values (NaN)#11432
PranavAchar01 wants to merge 2 commits into
pydata:mainfrom
PranavAchar01:fix/6431-pad-integer-nan

Conversation

@PranavAchar01

Copy link
Copy Markdown

Description

Fixes padding an integer array with an explicit non-finite constant_values (e.g. np.nan), which previously raised ValueError: cannot convert float NaN to integer:

import numpy as np, xarray as xr
da = xr.DataArray(np.arange(9), dims="x")
da.pad({"x": (0, 1)}, "constant", constant_values=np.nan)   # used to raise

Variable.pad only promoted the dtype for the default fill sentinel (dtypes.NA), not for an explicit np.nan. It now promotes the dtype (via np.result_type) when an explicit non-finite fill value cannot be represented by an integer dtype, matching the promotion already done for the default fill value. Finite fill values keep numpy's existing casting behaviour (e.g. constant_values=1.23456 still truncates on an integer array).

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code — prompted to locate and fix issue Bug when padding coordinates with NaNs #6431 with a localized change in Variable.pad plus a regression test. I have reviewed and tested the change locally (the pad test suites pass).

…values

Variable.pad only promoted the dtype for the default fill value (dtypes.NA),
so padding an integer array with an explicit np.nan raised
'cannot convert float NaN to integer'. Promote the dtype when an explicit
non-finite fill value can't be represented by an integer dtype, matching the
default-fill behaviour. Finite fills keep numpy's casting behaviour.

Closes pydata#6431
Copilot AI review requested due to automatic review settings July 3, 2026 07:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes DataArray/Dataset/Variable.pad(..., mode="constant", constant_values=np.nan) on integer-backed arrays by promoting the target dtype so non-finite fill values (NaN/Inf) can be represented, instead of raising during padding.

Changes:

  • Update Variable.pad to promote integer dtypes when an explicit non-finite constant_values is provided.
  • Add regression tests covering explicit constant_values=np.nan and mixed tuple fills (e.g. (0, np.nan)).
  • Document the behavior change in doc/whats-new.rst.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
xarray/core/variable.py Adds dtype-promotion logic for explicit non-finite constant_values in constant padding mode.
xarray/tests/test_dataarray.py Adds regression assertions for padding integer arrays with explicit NaN fill values.
doc/whats-new.rst Notes the bug fix and dtype-promotion behavior for non-finite explicit constant_values.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread xarray/core/variable.py
Comment on lines +1308 to +1312
if (
mode == "constant"
and constant_values is not None
and np.issubdtype(dtype, np.integer)
):
Only inspect plain floating scalars for non-finiteness so unit-aware duck
arrays (e.g. pint quantities) passed as constant_values are left untouched,
fixing a UnitStrippedWarning in test_pad_unit_constant_value.
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.

Bug when padding coordinates with NaNs

2 participants