Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ def _from_sequence(
result = scalars._data
result = lib.ensure_string_array(result, copy=copy, convert_na_value=False)
pa_arr = pa.array(result, mask=na_values, type=pa.large_string())
elif isinstance(scalars, ArrowExtensionArray):
pa_type = scalars._pa_array.type
# Use PyArrow's native cast for integer, string, and boolean types.
# Float has different representation in PyArrow: 1.0 -> "1" instead
# of "1.0", and uses different scientific notation (1e+10 vs 1e10).
# Boolean needs capitalize (true -> True, false -> False).
if (
pa.types.is_integer(pa_type)
or pa.types.is_large_string(pa_type)
or pa.types.is_string(pa_type)
or pa.types.is_boolean(pa_type)
):
pa_arr = pc.cast(scalars._pa_array, pa.large_string())
if pa.types.is_boolean(pa_type):
pa_arr = pc.utf8_capitalize(pa_arr)
else:
# Fall back for types where PyArrow's string representation
# differs from Python's str()
result = lib.ensure_string_array(scalars, copy=copy)
pa_arr = pa.array(result, type=pa.large_string(), from_pandas=True)
elif isinstance(scalars, (pa.Array, pa.ChunkedArray)):
pa_arr = pc.cast(scalars, pa.large_string())
else:
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/copy_view/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import numpy as np
import pytest

from pandas.compat import HAS_PYARROW

from pandas import (
DataFrame,
Series,
Expand Down Expand Up @@ -218,10 +216,12 @@ def test_convert_dtypes(using_infer_string):
df_orig = df.copy()
df2 = df.convert_dtypes()

if HAS_PYARROW:
assert not tm.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
if using_infer_string:
# String column is already Arrow-backed, so memory is shared
assert tm.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
# String column converts from object to Arrow, no memory sharing
assert not tm.shares_memory(get_array(df2, "a"), get_array(df, "a"))
assert tm.shares_memory(get_array(df2, "d"), get_array(df, "d"))
assert tm.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert tm.shares_memory(get_array(df2, "c"), get_array(df, "c"))
Expand Down
Loading