{"record":{"id":"c68e9fd7614d15b5","repo":"pandas-dev/pandas","slug":"cannot-cast-type-self-name-to-dtype-dtype-c68e9f","errorCode":null,"errorMessage":"Cannot cast {type(self).__name__} to dtype {dtype}","messagePattern":"Cannot cast (.+?) to dtype (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":971,"sourceCode":"                new_left = Index(self._left, copy=False).astype(dtype.subtype)\n                new_right = Index(self._right, copy=False).astype(dtype.subtype)\n            except IntCastingNaNError:\n                # e.g test_subtype_integer\n                raise\n            except (TypeError, ValueError) as err:\n                # e.g. test_subtype_integer_errors f8->u8 can be lossy\n                #  and raises ValueError\n                msg = (\n                    f\"Cannot convert {self.dtype} to {dtype}; subtypes are incompatible\"\n                )\n                raise TypeError(msg) from err\n            return self._shallow_copy(new_left, new_right)\n        else:\n            try:\n                return super().astype(dtype, copy=copy)\n            except (TypeError, ValueError) as err:\n                msg = f\"Cannot cast {type(self).__name__} to dtype {dtype}\"\n                raise TypeError(msg) from err\n\n    def equals(self, other) -> bool:\n        if type(self) != type(other):\n            return False\n\n        return bool(\n            self.closed == other.closed\n            and self.left.equals(other.left)\n            and self.right.equals(other.right)\n        )\n\n    @classmethod\n    def _concat_same_type(cls, to_concat: Sequence[IntervalArray]) -> Self:\n        \"\"\"\n        Concatenate multiple IntervalArray\n\n        Parameters\n        ----------","sourceCodeStart":953,"sourceCodeEnd":989,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/interval.py#L953-L989","documentation":"Raised in IntervalArray.astype's else-branch: the target dtype is not an IntervalDtype and the parent ExtensionArray.astype raised TypeError or ValueError. IntervalArrays cannot be cast directly to plain numeric, string, or object dtypes because the interval structure has no such representation.","triggerScenarios":"arr.astype('int64'); arr.astype('float64'); arr.astype(str); arr.astype(object) directly on an IntervalArray.","commonSituations":"Trying to extract endpoint numerics by casting the whole array; misunderstanding that endpoints live on .left/.right.","solutions":["Cast endpoints separately: arr.left.astype(...) or arr.right.astype(...).","Use arr.to_tuples() for an object/tuple representation.","Materialize as object ndarray via np.asarray(arr) when you really need Interval objects."],"exampleFix":"// before\narr.astype('int64')\n// after\narr.left.astype('int64')","handlingStrategy":"type-guard","validationCode":"from pandas import IntervalDtype\nfrom pandas.api.types import pandas_dtype\n\ndef cast_interval_endpoints(arr, target):\n    return arr.left.astype(target), arr.right.astype(target)","typeGuard":"from pandas import IntervalDtype\nfrom pandas.api.types import pandas_dtype\n\ndef is_interval_dtype_target(target):\n    return isinstance(pandas_dtype(target), IntervalDtype)","tryCatchPattern":null,"preventionTips":["Never astype an IntervalArray to a non-interval dtype to extract endpoints.","Use .left / .right for endpoint access and casting.","Use to_tuples() or np.asarray(arr) for object/tuple representations."],"tags":["interval-array","astype","endpoints"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}