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 numberDiff line numberDiff 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())
elifisinstance(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)
orpa.types.is_large_string(pa_type)
orpa.types.is_string(pa_type)
orpa.types.is_boolean(pa_type)
):
pa_arr=pc.cast(scalars._pa_array, pa.large_string())
ifpa.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)
elifisinstance(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 numberDiff line numberDiff line change
Expand Up@@ -3,8 +3,6 @@
importnumpyasnp
importpytest

frompandas.compatimportHAS_PYARROW

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

ifHAS_PYARROW:
assertnottm.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
ifusing_infer_string:
# String column is already Arrow-backed, so memory is shared
asserttm.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
# String column converts from object to Arrow, no memory sharing
assertnottm.shares_memory(get_array(df2, "a"), get_array(df, "a"))
asserttm.shares_memory(get_array(df2, "d"), get_array(df, "d"))
asserttm.shares_memory(get_array(df2, "b"), get_array(df, "b"))
asserttm.shares_memory(get_array(df2, "c"), get_array(df, "c"))
Expand Down
Loading