pandas-dev/pandas · error · TypeError
Cannot convert {self.dtype} to {dtype}; subtypes are incompa
Error message
Cannot convert {self.dtype} to {dtype}; subtypes are incompatible What it means
Raised as a TypeError by `IntervalArray.astype` when the current subtype is a float dtype and the target IntervalDtype's subtype is an i8-convertible datetime-like (e.g., `datetime64[ns]` or `timedelta64[ns]`). Casting float NaN positions into i8 datetimes would silently corrupt NaT, so it is disallowed on the array even though `Index.astype` permits it. Fires at pandas/core/arrays/interval.py:947.
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 71959b8cb9)
Solutions
- Cast the underlying Index to datetime first: `left = pd.to_datetime(pd.Index(ia.left)); right = pd.to_datetime(pd.Index(ia.right))` then rebuild.
- Convert epoch floats via `pd.to_datetime(ia.left, unit='s')`.
- Build a fresh `pd.IntervalIndex.from_arrays(left_ts, right_ts, closed=ia.closed)`.
Example fix
// before
ia.astype('interval[datetime64[ns]]')
// after
left = pd.to_datetime(pd.Index(ia.left), unit='s')
right = pd.to_datetime(pd.Index(ia.right), unit='s')
pd.IntervalIndex.from_arrays(left, right, closed=ia.closed) Defensive patterns
Strategy: validation
Validate before calling
import pandas as pd
def interval_to_datetime(ia):
left = pd.to_datetime(pd.Index(ia.left))
right = pd.to_datetime(pd.Index(ia.right))
return pd.IntervalIndex.from_arrays(left, right, closed=ia.closed) Type guard
import pandas as pd
from pandas.core.dtypes.common import is_float_dtype
def is_float_subtype(ia) -> bool:
return is_float_dtype(ia.dtype.subtype) Try / catch
try:
out = ia.astype('interval[datetime64[ns]]')
except TypeError as e:
if "subtypes are incompatible" in str(e):
left = pd.to_datetime(pd.Index(ia.left))
right = pd.to_datetime(pd.Index(ia.right))
out = pd.IntervalIndex.from_arrays(left, right, closed=ia.closed)
else:
raise Prevention
- Convert numeric epoch bounds to datetime explicitly before building intervals.
- Do not rely on IntervalArray.astype to bridge float -> datetime subtypes.
- Keep interval subtype transitions within numeric or within datetime-like families.
When it happens
Trigger: `ia.astype('interval[datetime64[ns]]')` where `ia.dtype.subtype` is float64.
Common situations: Trying to reinterpret numeric interval bounds (e.g., epoch floats) as datetime intervals in one step.
Related errors
- Cannot cast {type(self).__name__} to dtype {dtype}
- Converting from {self.dtype} to {dtype} is not supported. Do
- Cannot cast {type(self).__name__} to dtype {dtype}
- value should be a '{self._scalar_type.__name__}', 'NaT', or
- {dtype=} does not have a resolution.
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/0b1e16469b67165f.
Report an issue: GitHub.