pandas-dev/pandas · error · TypeError

Left and right arrays must have matching signedness. Got

Error message

Left and right arrays must have matching signedness. Got {left_dtype} and {right_dtype}.

What it means

Raised at the end of `_ensure_simple_new_inputs` after all dtype coercion: if both endpoints are integer-kind (`kind in 'iu'`) but one is signed and the other unsigned (e.g. int64 vs uint64), the signedness mismatch is rejected because IntervalArray cannot pick a single canonical integer dtype and silent overflow would occur.

Solutions

  1. Cast both endpoints to the same signed integer dtype: `right.astype('int64')` (or both to unsigned if values fit).
  2. Cast both to float64 if values may exceed the signed range.
  3. Normalise at ingestion: ensure both bounds use `np.int64` before constructing.

Example fix

# before
IntervalArray.from_arrays(np.array([0,1], dtype='int64'), np.array([1,2], dtype='uint64'))

# after
IntervalArray.from_arrays(np.array([0,1], dtype='int64'), np.array([1,2], dtype='int64'))
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def unify_signedness(left, right):
    lk, rk = left.dtype.kind, right.dtype.kind
    if lk in 'iu' and rk in 'iu' and lk != rk:
        right = right.astype(left.dtype)
    return left, right

Type guard

def matching_signedness(left, right) -> bool:
    lk, rk = left.dtype.kind, right.dtype.kind
    return not (lk in 'iu' and rk in 'iu' and lk != rk)

Try / catch

try:
    arr = IntervalArray.from_arrays(left, right)
except TypeError as e:
    if 'matching signedness' in str(e):
        right = right.astype(left.dtype)
        arr = IntervalArray.from_arrays(left, right)
    else:
        raise

Prevention

When it happens

Trigger: `IntervalArray.from_arrays(np.array([0,1], dtype='int64'), np.array([1,2], dtype='uint64'))`; mixing numpy default int with explicitly-typed unsigned arrays from ctypes/cython layers; downcasting from int64 to uint32 on one side via astype.

Common situations: Interfacing with libraries that emit unsigned arrays (hash codes, bitmask endpoints); explicit `dtype='uint32'` on one bound only; cross-platform int width differences.

Related errors


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

Appendix: source

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

            lbase = getattr(left, "_ndarray", left)
            lbase = getattr(lbase, "_data", lbase).base
            rbase = getattr(right, "_ndarray", right)
            rbase = getattr(rbase, "_data", rbase).base
            if lbase is not None and lbase is rbase:
                # If these share data, then setitem could corrupt our IA
                right = right.copy()

        dtype = IntervalDtype(left.dtype, closed=closed)

        # Check for mismatched signed/unsigned integer dtypes after casting
        left_dtype = left.dtype
        right_dtype = right.dtype
        if (
            left_dtype.kind in "iu"
            and right_dtype.kind in "iu"
            and left_dtype.kind != right_dtype.kind
        ):
            raise TypeError(
                f"Left and right arrays must have matching signedness. "
                f"Got {left_dtype} and {right_dtype}."
            )
        return left, right, dtype

    @classmethod
    def _from_sequence(
        cls,
        scalars,
        *,
        dtype: Dtype | None = None,
        copy: bool = False,
    ) -> Self:
        return cls(scalars, dtype=dtype, copy=copy)

    @classmethod
    def _from_factorized(cls, values: np.ndarray, original: IntervalArray) -> Self:
        return cls._from_sequence(values, dtype=original.dtype)

View on GitHub (pinned to 3b7651241d)