pandas-dev/pandas · error · TypeError

Cannot set float NaN to integer-backed IntervalArray

Error message

Cannot set float NaN to integer-backed IntervalArray

What it means

Raised in IntervalArray._validate_setitem_value when the value is a valid NA per is_valid_na_for_dtype AND the array's subtype is integer. NumPy integer arrays cannot hold NaN; pandas raises TypeError (GH#45484) instead of silently coercing, mirroring the behavior for any non-NA un-holdable value.

Solutions

  1. Use pd.NA instead of np.nan (handled via the mask).
  2. Cast the array to a float subtype if NaN semantics are required.
  3. Use a nullable integer backing (IntervalArray over Int64 etc.).

Example fix

// before
int_arr[0] = np.nan
// after
int_arr[0] = pd.NA
Defensive patterns

Strategy: type-guard

Validate before calling

import numpy as np, pandas as pd
from pandas.api.types import is_integer_dtype

def safe_missing(arr):
    return pd.NA if is_integer_dtype(arr.dtype.subtype) else np.nan

Type guard

import numpy as np, pandas as pd
from pandas.api.types import is_integer_dtype

def normalize_na(v, arr):
    if v is np.nan and is_integer_dtype(arr.dtype.subtype):
        return pd.NA
    return v

Prevention

When it happens

Trigger: int_backed_arr[0] = np.nan; int_backed_arr.fillna(np.nan); assigning float NaN to integer-backed interval arrays.

Common situations: Code that uses np.nan uniformly across dtypes; converting nullable data to integer-backed intervals; pipelines that assume NaN is always acceptable as missing.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/436ccf8bb5a548f3. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/interval.py:1199

            left = right = self.left._na_value
        else:
            raise TypeError(
                "can only insert Interval objects and NA into an IntervalArray"
            )
        return left, right

    def _validate_setitem_value(self, value):
        if is_list_like(value):
            return self._validate_listlike(value)

        left, right = self._validate_scalar(value)

        if is_valid_na_for_dtype(value, self.left.dtype):
            if is_integer_dtype(self.dtype.subtype):
                # can't set NaN on a numpy integer array
                # GH#45484 TypeError, not ValueError, matches what we get with
                #  non-NA un-holdable value.
                raise TypeError("Cannot set float NaN to integer-backed IntervalArray")

        return left, right

    # ---------------------------------------------------------------------
    # Rendering Methods

    def _formatter(self, boxed: bool = False) -> Callable[[object], str]:
        # returning 'str' here causes us to render as e.g. "(0, 1]" instead of
        #  "Interval(0, 1, closed='right')"
        return str

    # ---------------------------------------------------------------------
    # Vectorized Interval Properties/Attributes

    @property
    def left(self) -> Index:
        """
        Return the left endpoints of each Interval in the IntervalArray as an Index.

View on GitHub (pinned to 3b7651241d)