diff --git a/pandas/core/arrays/arrow/array.py b/pandas/core/arrays/arrow/array.py index 0b90bcea35100..7e7d7e8f83bc9 100644 --- a/pandas/core/arrays/arrow/array.py +++ b/pandas/core/arrays/arrow/array.py @@ -185,6 +185,7 @@ def floordiv_compat( ArrayLike, AxisInt, Dtype, + DtypeObj, FillnaOptions, InterpolateOptions, Iterator, @@ -313,6 +314,18 @@ def __init__(self, values: pa.Array | pa.ChunkedArray) -> None: ) self._dtype = ArrowDtype(self._pa_array.type) + @classmethod + def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self: + try: + pa_array = cls._from_sequence(scalars, dtype=dtype) + except pa.ArrowNotImplementedError: + # _from_scalars should only raise ValueError or TypeError. + raise ValueError + + if lib.infer_dtype(scalars, skipna=True) != lib.infer_dtype(pa_array, skipna=True): + raise ValueError + return pa_array + @classmethod def _from_sequence( cls, scalars, *, dtype: Dtype | None = None, copy: bool = False diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 9668981df827b..ac04d7eb1c751 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -53,6 +53,7 @@ from pandas._typing import ( ArrayLike, Dtype, + DtypeObj, NpDtype, Self, npt, @@ -180,6 +181,12 @@ def __len__(self) -> int: """ return len(self._pa_array) + @classmethod + def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self: + if lib.infer_dtype(scalars, skipna=True) not in ["string", "empty"]: + raise ValueError + return cls._from_sequence(scalars, dtype=dtype) + @classmethod def _from_sequence( cls, scalars, *, dtype: Dtype | None = None, copy: bool = False