{"record":{"id":"000b116235278e0d","repo":"pandas-dev/pandas","slug":"not-supported-to-convert-intervalarray-to-type-wit","errorCode":null,"errorMessage":"Not supported to convert IntervalArray to type with different 'subtype' ({self.dtype.subtype} vs {type.subtype}) and 'closed' ({self.closed} vs {type.closed}) attributes","messagePattern":"Not supported to convert IntervalArray to type with different 'subtype' \\((.+?) vs (.+?)\\) and 'closed' \\((.+?) vs (.+?)\\) attributes","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":1625,"sourceCode":"        )\n        mask = self.isna()\n        if mask.any():\n            # if there are missing values, set validity bitmap also on the array level\n            null_bitmap = pyarrow.array(~mask).buffers()[1]\n            storage_array = pyarrow.StructArray.from_buffers(\n                storage_array.type,\n                len(storage_array),\n                [null_bitmap],\n                children=[storage_array.field(0), storage_array.field(1)],\n            )\n\n        if type is not None:\n            if type.equals(interval_type.storage_type):\n                return storage_array\n            elif isinstance(type, ArrowIntervalType):\n                # ensure we have the same subtype and closed attributes\n                if not type.equals(interval_type):\n                    raise TypeError(\n                        \"Not supported to convert IntervalArray to type with \"\n                        f\"different 'subtype' ({self.dtype.subtype} vs {type.subtype}) \"\n                        f\"and 'closed' ({self.closed} vs {type.closed}) attributes\"\n                    )\n            else:\n                raise TypeError(\n                    f\"Not supported to convert IntervalArray to '{type}' type\"\n                )\n\n        return pyarrow.ExtensionArray.from_storage(interval_type, storage_array)\n\n    def to_tuples(self, na_tuple: bool = True) -> np.ndarray:\n        \"\"\"\n        Return an ndarray (if self is IntervalArray) or Index \\\n        (if self is IntervalIndex) of tuples of the form (left, right).\n\n        This method extracts the bounds of each interval as a tuple,\n        useful for iteration or conversion to other data structures.","sourceCodeStart":1607,"sourceCodeEnd":1643,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L1607-L1643","documentation":"Raised by IntervalArray.__arrow_array__ when the caller passes an explicit target pyarrow type that is an ArrowIntervalType but does not equal the array's natural interval_type — i.e. its subtype or closed differs from the source. The library refuses to silently reinterpret closure or precision during conversion.","triggerScenarios":"Calling pa.array(arr, type=ArrowIntervalType(pa.int32(), 'left')) on an array whose subtype is int64 and closed='right', or any mismatched (subtype, closed) pair.","commonSituations":"Schema-driven ETL pipelines that pass a fixed ArrowIntervalType without aligning it to the source, or migrations that change closure conventions.","solutions":["Align the target ArrowIntervalType's subtype and closed with the source: pa.array(arr, type=ArrowIntervalType(pa.from_numpy_dtype(arr.dtype.subtype), arr.closed)).","Drop the explicit type argument and let pandas infer it.","Re-cast arr (arr.astype / arr.set_closed) so it matches the target schema before conversion."],"exampleFix":"# before\nfrom pandas.core.arrays.arrow.extension_types import ArrowIntervalType\ntarget = ArrowIntervalType(pa.int32(), 'left')\npa.array(arr, type=target)  # arr is interval[int64, right]\n\n# after\ntarget = ArrowIntervalType(pa.int64(), 'right')\npa.array(arr, type=target)","handlingStrategy":"validation","validationCode":"import pyarrow as pa\nfrom pandas.core.arrays.arrow.extension_types import ArrowIntervalType\n\ndef matching_arrow_type(arr):\n    subtype = pa.from_numpy_dtype(arr.dtype.subtype)\n    return ArrowIntervalType(subtype, arr.closed)","typeGuard":"def arrow_type_matches(arr, target) -> bool:\n    import pyarrow as pa\n    from pandas.core.arrays.arrow.extension_types import ArrowIntervalType\n    if not isinstance(target, ArrowIntervalType):\n        return False\n    return (target.subtype == pa.from_numpy_dtype(arr.dtype.subtype)\n            and target.closed == arr.closed)","tryCatchPattern":null,"preventionTips":["Derive the target ArrowIntervalType from the source array instead of hardcoding it.","Keep closure conventions consistent between Arrow schema and pandas source.","Unit-test schema conversion against representative samples."],"tags":["interval-array","pyarrow","schema","interop"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}