{"record":{"id":"60f4515bc9f2e78b","repo":"pandas-dev/pandas","slug":"expected-array-of-self-type-got-array-type-in","errorCode":null,"errorMessage":"Expected array of {self} type, got {array.type} instead","messagePattern":"Expected array of (.+?) type, got (.+?) instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/numeric.py","lineNumber":91,"sourceCode":"        import pyarrow\n\n        from pandas.core.arrays.arrow._arrow_utils import (\n            pyarrow_array_to_numpy_and_mask,\n        )\n\n        array_class = self.construct_array_type()\n\n        pyarrow_type = pyarrow.from_numpy_dtype(self.type)\n        if not array.type.equals(pyarrow_type) and not pyarrow.types.is_null(\n            array.type\n        ):\n            # test_from_arrow_type_error raise for string, but allow\n            #  through itemsize conversion GH#31896\n            rt_dtype = pandas_dtype(array.type.to_pandas_dtype())\n            if rt_dtype.kind not in \"iuf\":\n                # Could allow \"c\" or potentially disallow float<->int conversion,\n                #  but at the moment we specifically test that uint<->int works\n                raise TypeError(\n                    f\"Expected array of {self} type, got {array.type} instead\"\n                )\n\n            array = array.cast(pyarrow_type)\n\n        if isinstance(array, pyarrow.ChunkedArray):\n            array = array.combine_chunks()\n\n        data, mask = pyarrow_array_to_numpy_and_mask(array, dtype=self.numpy_dtype)\n        if data.dtype.kind == \"f\" and is_nan_na():\n            mask[np.isnan(data)] = False\n        return array_class(data.copy(), ~mask, copy=False)\n\n    @classmethod\n    def _get_dtype_mapping(cls) -> Mapping[np.dtype, NumericDtype]:\n        raise AbstractMethodError(cls)\n\n    @classmethod","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/numeric.py#L73-L109","documentation":"Raised by NumericDtype.__from_arrow__ when constructing IntegerArray/FloatingArray from a pyarrow Array/ChunkedArray whose type, after round-trip to pandas dtype, is not an integer/unsigned/float kind (not in 'iuf'). pandas can convert itemsize but refuses genuinely incompatible types (e.g. strings) rather than producing invalid data (GH#31896 context).","triggerScenarios":"Calling pa.Table.from_pandas / pd.array(arr, dtype='Int64') with a pyarrow string/binary/list array; converting a pyarrow table whose column is string into a nullable Int64 column directly via from_arrow.","commonSituations":"Mixing pyarrow and pandas dtypes; reading arrow data whose logical type does not match the target masked numeric dtype; assuming pyarrow will silently cast strings to ints.","solutions":["Cast the pyarrow array to a compatible type first: arr.cast(pa.int64()) before constructing the masked array.","Parse/convert the source data to numeric before building the arrow array.","Target the dtype matching the arrow type, or use object/string dtype explicitly."],"exampleFix":"// before\npa.array(['1','2'], type=pa.string())  # -> from_arrow(Int64) raises\n\n// after\npa.array(['1','2'], type=pa.string()).cast(pa.int32())  # then from_arrow(Int32)","handlingStrategy":"type-guard","validationCode":"import pyarrow as pa\n\ndef arrow_to_masked_numeric(array, target_dtype_cls):\n    pa_type = pa.from_numpy_dtype(target_dtype_cls.type)\n    if not (array.type.equals(pa_type) or pa.types.is_null(array.type)):\n        rt = array.type.to_pandas_dtype()\n        import numpy as np\n        if np.dtype(rt).kind not in \"iuf\":\n            raise TypeError(f\"incompatible arrow type {array.type}\")\n        array = array.cast(pa_type)\n    return target_dtype_cls.__from_arrow__(array)","typeGuard":"def arrow_type_is_numeric_compat(array) -> bool:\n    import pyarrow as pa, numpy as np\n    try:\n        return np.dtype(array.type.to_pandas_dtype()).kind in \"iuf\"\n    except Exception:\n        return False","tryCatchPattern":"try:\n    arr = dtype_cls.__from_arrow__(pa_array)\nexcept TypeError as e:\n    if \"Expected array of\" in str(e):\n        import pyarrow as pa\n        arr = dtype_cls.__from_arrow__(pa_array.cast(pa.from_numpy_dtype(dtype_cls.type)))\n    else:\n        raise","preventionTips":["Cast pyarrow arrays to the target numpy dtype before from_arrow.","Validate arrow type kind against 'iuf' before numeric conversion.","Prefer pd.array(pa_array, dtype=...) for user-facing conversions."],"tags":["numeric-array","from-arrow","pyarrow","dtype"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}