pandas-dev/pandas · error · TypeError

Expected array of type, got instead

Error message

Expected array of {self} type, got {array.type} instead

What it means

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.

Solutions

  1. Cast the pyarrow array to a numeric type first: array.cast(pa.int64()).
  2. Use the appropriate pandas dtype for the source type (e.g. string, datetime).
  3. Validate array.type before passing to __from_arrow__ and route non-numeric types elsewhere.

Example fix

// before
NumericArray._from_arrow(pa_string_array)   # raises
// after
casted = pa_string_array.cast(pa.int64())
NumericArray._from_arrow(casted)
Defensive patterns

Strategy: validation

Validate before calling

import pyarrow as pa
from pandas.api.types import pandas_dtype
rt_kind = pandas_dtype(array.type.to_pandas_dtype()).kind
if not array.type.equals(pa.from_numpy_dtype(target_np_dtype)) and rt_kind not in 'iuf':
    array = array.cast(pa.from_numpy_dtype(target_np_dtype))

Type guard

def arrow_type_is_numeric_compatible(array) -> bool:
    from pandas.api.types import pandas_dtype
    return pandas_dtype(array.type.to_pandas_dtype()).kind in 'iuf'

Try / catch

try:
    out = NumericArray._from_arrow(array)
except TypeError as e:
    if 'Expected array of' in str(e):
        import pyarrow as pa
        out = NumericArray._from_arrow(array.cast(pa.int64()))
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/60f4515bc9f2e78b. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/numeric.py:91

        import pyarrow

        from pandas.core.arrays.arrow._arrow_utils import (
            pyarrow_array_to_numpy_and_mask,
        )

        array_class = self.construct_array_type()

        pyarrow_type = pyarrow.from_numpy_dtype(self.type)
        if not array.type.equals(pyarrow_type) and not pyarrow.types.is_null(
            array.type
        ):
            # test_from_arrow_type_error raise for string, but allow
            #  through itemsize conversion GH#31896
            rt_dtype = pandas_dtype(array.type.to_pandas_dtype())
            if rt_dtype.kind not in "iuf":
                # Could allow "c" or potentially disallow float<->int conversion,
                #  but at the moment we specifically test that uint<->int works
                raise TypeError(
                    f"Expected array of {self} type, got {array.type} instead"
                )

            array = array.cast(pyarrow_type)

        if isinstance(array, pyarrow.ChunkedArray):
            array = array.combine_chunks()

        data, mask = pyarrow_array_to_numpy_and_mask(array, dtype=self.numpy_dtype)
        if data.dtype.kind == "f" and is_nan_na():
            mask[np.isnan(data)] = False
        return array_class(data.copy(), ~mask, copy=False)

    @classmethod
    def _get_dtype_mapping(cls) -> Mapping[np.dtype, NumericDtype]:
        raise AbstractMethodError(cls)

    @classmethod

View on GitHub (pinned to 3b7651241d)