Skip to content

BUG: pd.array raising with NumPy array and large dtype #52591

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

Merged
merged 7 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 0 deletions doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Bug fixes
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
- Bug in :func:`pandas.testing.assert_series_equal` where ``check_dtype=False`` would still raise for datetime or timedelta types with different resolutions (:issue:`52449`)
- Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`)
- Bug in :meth:`pd.array` raising for ``NumPy`` array and ``pa.large_string`` or ``pa.large_binary`` (:issue:`52590`)

.. ---------------------------------------------------------------------------
.. _whatsnew_201.other:
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
Construct a new ExtensionArray from a sequence of scalars.
"""
pa_dtype = to_pyarrow_type(dtype)
if (
isinstance(scalars, np.ndarray)
and isinstance(dtype, ArrowDtype)
and (
pa.types.is_large_binary(pa_dtype) or pa.types.is_large_string(pa_dtype)
)
):
scalars = scalars.tolist()
Copy link
Member

Choose a reason for hiding this comment

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

Could you open an issue in arrow and link that issue here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done


if isinstance(scalars, cls):
scalars = scalars._pa_array
elif not isinstance(scalars, (pa.Array, pa.ChunkedArray)):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,20 @@ def test_setitem_boolean_replace_with_mask_segfault():
assert arr._pa_array == expected._pa_array


@pytest.mark.parametrize(
"data, arrow_dtype",
[
([b"a", b"b"], pa.large_binary()),
(["a", "b"], pa.large_string()),
],
)
def test_conversion_large_dtypes_from_numpy_array(data, arrow_dtype):
dtype = ArrowDtype(arrow_dtype)
result = pd.array(np.array(data), dtype=dtype)
expected = pd.array(data, dtype=dtype)
tm.assert_extension_array_equal(result, expected)


@pytest.mark.parametrize("pa_type", tm.ALL_INT_PYARROW_DTYPES + tm.FLOAT_PYARROW_DTYPES)
def test_describe_numeric_data(pa_type):
# GH 52470
Expand Down