diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 4025435f8d461..47f477a9a4e92 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -750,7 +750,6 @@ cpdef ndarray[object] ensure_string_array( out = arr.astype(str).astype(object) out[arr.isna()] = na_value return out - arr = arr.to_numpy() elif not util.is_array(arr): arr = np.array(arr, dtype="object") diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index d3a64055f6c10..130f61b68914d 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -351,6 +351,9 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal result[na_values] = libmissing.NA else: + if hasattr(scalars, "type"): + # pyarrow array + scalars = np.array(scalars) # convert non-na-likes to str, and nan-likes to StringDtype().na_value result = lib.ensure_string_array(scalars, na_value=libmissing.NA, copy=copy) diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 4d2b39ec61fca..1eea95d02b6d4 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -144,6 +144,8 @@ def _from_sequence(cls, scalars, dtype: Dtype | None = None, copy: bool = False) result = scalars._data result = lib.ensure_string_array(result, copy=copy, convert_na_value=False) return cls(pa.array(result, mask=na_values, type=pa.string())) + elif isinstance(scalars, (pa.Array, pa.ChunkedArray)): + return cls(pc.cast(scalars, pa.string())) # convert non-na-likes to str result = lib.ensure_string_array(scalars, copy=copy) diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index ebe1799635226..f2c376b9f4fe7 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -2324,3 +2324,11 @@ def test_boolean_reduce_series_all_null(all_boolean_reductions, skipna): else: expected = pd.NA assert result is expected + + +@pytest.mark.parametrize("dtype", ["string", "string[pyarrow]"]) +def test_series_from_string_array(dtype): + arr = pa.array("the quick brown fox".split()) + ser = pd.Series(arr, dtype=dtype) + expected = pd.Series(ArrowExtensionArray(arr), dtype=dtype) + tm.assert_series_equal(ser, expected)