{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/numeric.py#L73-L109","documentation":"Raised by NumericDtype.__from_arrow__ when a pyarrow array's type does not match the expected numeric type and the round-tripped pandas dtype kind is not in 'iuf'. pandas allows cross-size int/float/uint casts but rejects conversions from non-numeric arrow types (e.g. string, timestamp, list) into a NumericArray.","triggerScenarios":"Constructing a pandas nullable numeric array from a pyarrow array whose type is non-numeric and not null: pd.array(arr, dtype='Int64') where arr is a pyarrow string/timestamp/array type.","commonSituations":"Reading Arrow/Parquet data where a column has an unexpected type; explicit casts from arrow string arrays to Int/Float; interop with Arrow sources that emit timestamp or decimal where numeric was expected.","solutions":["Cast the pyarrow array to a numeric type first: array.cast(pa.int64()).","Use the appropriate pandas dtype for the source type (e.g. string, datetime).","Validate array.type before passing to __from_arrow__ and route non-numeric types elsewhere."],"exampleFix":"// before\nNumericArray._from_arrow(pa_string_array)   # raises\n// after\ncasted = pa_string_array.cast(pa.int64())\nNumericArray._from_arrow(casted)","handlingStrategy":"validation","validationCode":"import pyarrow as pa\nfrom pandas.api.types import pandas_dtype\nrt_kind = pandas_dtype(array.type.to_pandas_dtype()).kind\nif not array.type.equals(pa.from_numpy_dtype(target_np_dtype)) and rt_kind not in 'iuf':\n    array = array.cast(pa.from_numpy_dtype(target_np_dtype))","typeGuard":"def arrow_type_is_numeric_compatible(array) -> bool:\n    from pandas.api.types import pandas_dtype\n    return pandas_dtype(array.type.to_pandas_dtype()).kind in 'iuf'","tryCatchPattern":"try:\n    out = NumericArray._from_arrow(array)\nexcept TypeError as e:\n    if 'Expected array of' in str(e):\n        import pyarrow as pa\n        out = NumericArray._from_arrow(array.cast(pa.int64()))\n    else:\n        raise","preventionTips":["Cast pyarrow arrays to a numeric type before constructing pandas nullable arrays.","Inspect array.type.to_pandas_dtype().kind at Arrow interop boundaries.","Route non-numeric Arrow columns to the appropriate pandas dtype."],"tags":["pandas","numeric-array","arrow","dtype","type-coercion"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}