Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix regression when using Series with arrow string array #52076

Merged
merged 2 commits into from
Mar 22, 2023
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
1 change: 0 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,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")
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
result[na_values] = libmissing.NA

else:
if hasattr(scalars, "type"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we do an actual isinstance check?

If we have some helper method is_pyarrow_array like the following, this should be quite easy?

def is_pyarrow_array(obj):
    if _pyarrow_installed:
        return isinstance(obj, pa.Array)
    return False

with variables _pyarrow_installed and pa filled in once on first import.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR that does this -> #52830

# 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)

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,14 @@ def test_concat_empty_arrow_backed_series(dtype):
tm.assert_series_equal(result, 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)


# _data was renamed to _pa_data
class OldArrowExtensionArray(ArrowExtensionArray):
def __getstate__(self):
Expand Down