pandas-dev/pandas · error · TypeError
Cannot convert to ; subtypes are incompatible
Error message
Cannot convert {self.dtype} to {dtype}; subtypes are incompatible What it means
Raised in IntervalArray.astype when the target dtype is an IntervalDtype, the current subtype is floating, and the target subtype requires i8 (datetime-like) conversion. Reinterpreting float bounds as timestamps is allowed on IntervalIndex.astype but explicitly disallowed at the array level to avoid ambiguous casts.
Solutions
- Route through IntervalIndex if datetime semantics are genuinely required: pd.IntervalIndex(arr).astype(target).
- Cast endpoint Indexes to datetime explicitly, then rebuild the IntervalArray.
- Keep a numeric target subtype (float/integer) compatible with the float source.
Example fix
// before
arr.astype('interval[datetime64[ns]]')
// after
import pandas as pd
pd.IntervalIndex(arr).astype('interval[datetime64[ns]]') Defensive patterns
Strategy: validation
Validate before calling
from pandas.api.types import is_float_dtype, pandas_dtype
from pandas.core.dtypes.common import needs_i8_conversion
def interval_astype_safe(arr, target):
t = pandas_dtype(target)
from pandas import IntervalDtype
if isinstance(t, IntervalDtype) and is_float_dtype(arr.dtype.subtype) and needs_i8_conversion(t.subtype):
raise ValueError('Use IntervalIndex.astype for float->datetime-like interval casts')
return arr.astype(t) Try / catch
try:
arr.astype(target)
except TypeError as e:
if 'subtypes are incompatible' in str(e):
# route through IntervalIndex or pick a numeric subtype
pass
else:
raise Prevention
- Inspect arr.dtype.subtype before astype to an IntervalDtype target.
- Remember IntervalIndex.astype permits casts that IntervalArray.astype rejects.
- Avoid reinterpreting float bounds as datetime-like at the array level.
When it happens
Trigger: float_backed_arr.astype('interval[datetime64[ns]]'); arr.astype(IntervalDtype('datetime64[ns]')) on a float-backed IntervalArray; arr.astype('interval[timedelta64[ns]]') from float subtype.
Common situations: Reinterpreting numeric interval bounds as timestamps after dtype inference produced floats; mixing time-based and numeric interval pipelines.
Related errors
- Cannot cast to dtype
- Cannot use .astype to convert from timezone-aware dtype to…
- Casting to unit-less dtype 'datetime64' is not supported…
- Conversion to arrow with subtype
- 'value' should be a compatible interval type, got
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/0b1e16469b67165f.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/interval.py:947
ExtensionArray or NumPy ndarray with 'dtype' for its dtype.
"""
from pandas import Index
if dtype is not None:
dtype = pandas_dtype(dtype)
if isinstance(dtype, IntervalDtype):
if dtype == self.dtype:
return self.copy() if copy else self
if is_float_dtype(self.dtype.subtype) and needs_i8_conversion(
dtype.subtype
):
# This is allowed on the Index.astype but we disallow it here
msg = (
f"Cannot convert {self.dtype} to {dtype}; subtypes are incompatible"
)
raise TypeError(msg)
# need to cast to different subtype
try:
# We need to use Index rules for astype to prevent casting
# np.nan entries to int subtypes
new_left = Index(self._left, copy=False).astype(dtype.subtype)
new_right = Index(self._right, copy=False).astype(dtype.subtype)
except IntCastingNaNError:
# e.g test_subtype_integer
raise
except (TypeError, ValueError) as err:
# e.g. test_subtype_integer_errors f8->u8 can be lossy
# and raises ValueError
msg = (
f"Cannot convert {self.dtype} to {dtype}; subtypes are incompatible"
)
raise TypeError(msg) from err
return self._shallow_copy(new_left, new_right)View on GitHub (pinned to 3b7651241d)