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 when the inferred/computed left or right dtype is categorical, object, or string. IntervalArray only supports numeric, datetime, timedelta subtypes; category/object/string endpoints are explicitly rejected (GH 19016, GH 66518 extended the check to the right side too).

Solutions

  1. Convert endpoints to a supported numeric/datetime dtype first: `.astype('int64')`, `.astype('float64')`, or `pd.to_datetime`.
  2. If intervals are genuinely string-bounded, store them as object tuples or use a different structure (IntervalArray will not accept them).
  3. Strip the categorical: `.astype('object').astype('int64')` or use the underlying codes if the category is numeric.

Example fix

# before
IntervalArray.from_arrays(['a','b'], ['c','d'])

# after - encode as numeric if meaningful
IntervalArray.from_arrays(pd.Series(['a','b']).astype('category').cat.codes, pd.Series(['c','d']).astype('category').cat.codes)
Defensive patterns

Strategy: validation

Validate before calling

import pandas as pd
from pandas.api.types import is_string_dtype, is_categorical_dtype

def bounds_have_supported_dtype(left, right):
    bad = (is_categorical_dtype(left.dtype) or is_string_dtype(left.dtype)
           or is_string_dtype(right.dtype) or left.dtype == object)
    return not bad

Type guard

import numpy as np

def is_supported_interval_subtype(arr) -> bool:
    k = getattr(arr.dtype, 'kind', None)
    return k in 'iuf' or isinstance(arr.dtype, np.dtype) and arr.dtype.kind in 'Mm'

Try / catch

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

Prevention

When it happens

Trigger: Passing Python strings as endpoints (`IntervalArray.from_arrays(['a','b'],['c','d'])`); a column read as `object` or `string` dtype; categorical endpoints from groupby keys; mixed-type object arrays that survived `maybe_convert_objects`.

Common situations: Loading intervals from CSV where bounds come in as text; user assuming string-typed intervals (like IP ranges) are supported; constructing from a categorical feature engineered into bins.

Related errors


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

Appendix: 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 3b7651241d)