pandas-dev/pandas · error · TypeError

category, object, and string subtypes are not supported for

Error message

category, object, and string subtypes are not supported for IntervalArray

What it means

Raised as a TypeError when either side of an interval is a categorical, object, or string dtype. IntervalArray only supports numeric, datetime, or timedelta subtypes; strings/categories have no total ordering that is meaningful for interval arithmetic. Fires at pandas/core/arrays/interval.py:334 (GH 19016, GH 66518).

Source

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

            left = left.astype(right.dtype)

        if type(left) != type(right):
            msg = (
                f"must not have differing left [{type(left).__name__}] and "
                f"right [{type(right).__name__}] types"
            )
            raise ValueError(msg)
        if (
            isinstance(left.dtype, CategoricalDtype)
            or is_string_dtype(left.dtype)
            or is_string_dtype(right.dtype)
        ):
            # GH 19016, GH 66518: reject unsupported right-side dtypes too.
            msg = (
                "category, object, and string subtypes are not supported "
                "for IntervalArray"
            )
            raise TypeError(msg)
        if isinstance(left, ABCPeriodIndex):
            msg = "Period dtypes are not supported, use a PeriodIndex instead"
            raise ValueError(msg)
        if isinstance(left, ABCDatetimeIndex) and str(left.tz) != str(right.tz):
            msg = (
                "left and right must have the same time zone, got "
                f"'{left.tz}' and '{right.tz}'"
            )
            raise ValueError(msg)
        elif needs_i8_conversion(left.dtype) and left.unit != right.unit:
            # e.g. m8[s] vs m8[ms], try to cast to a common dtype GH#55714
            left_arr, right_arr = left._data._ensure_matching_resos(right._data)
            left = ensure_index(left_arr)
            right = ensure_index(right_arr)

        # For dt64/td64 we want DatetimeArray/TimedeltaArray instead of ndarray
        left = ensure_wrapped_if_datetimelike(left)
        left = extract_array(left, extract_numpy=True)

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Convert bounds to a numeric dtype: `left = pd.to_numeric(left)`, `right = pd.to_numeric(right)`.
  2. If the data is genuinely categorical labels, do not use IntervalArray — use a CategoricalIndex or `pd.cut` on numeric codes.
  3. Strip whitespace / parse dates: `pd.to_datetime(left)` if the bounds are timestamps stored as strings.

Example fix

// before
pd.IntervalIndex.from_arrays(df['low_str'], df['high_str'])
// after
pd.IntervalIndex.from_arrays(pd.to_numeric(df['low_str']), pd.to_numeric(df['high_str']))
Defensive patterns

Strategy: validation

Validate before calling

def to_numeric_bounds(left, right):
    import pandas as pd
    left = pd.to_numeric(left, errors='coerce')
    right = pd.to_numeric(right, errors='coerce')
    return left, right

Type guard

import pandas as pd
import numpy as np

def is_supported_subtype(arr) -> bool:
    return arr.dtype.kind in 'iufMm' or pd.api.types.is_datetime64_any_dtype(arr.dtype)

Try / catch

try:
    ia = pd.IntervalArray(left, right)
except TypeError as e:
    if "subtypes are not supported" in str(e):
        ia = pd.IntervalArray(pd.to_numeric(left), pd.to_numeric(right))
    else:
        raise

Prevention

When it happens

Trigger: `pd.IntervalIndex.from_arrays(['a','b'], ['c','d'])`, passing a Categorical column, passing a `string` dtype, or passing object-dtype arrays of strings.

Common situations: Reading heterogeneous CSV columns where interval bounds were inferred as object/string; building bins from label-encoded categoricals.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/097377ed10832f1a. Report an issue: GitHub.