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

TST: Fix shares_memory for arrow string dtype #55823

Merged
merged 4 commits into from
Nov 6, 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
13 changes: 11 additions & 2 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
is_float_dtype,
is_sequence,
is_signed_integer_dtype,
is_string_dtype,
is_unsigned_integer_dtype,
pandas_dtype,
)
Expand Down Expand Up @@ -1044,10 +1045,18 @@ def shares_memory(left, right) -> bool:
if isinstance(left, pd.core.arrays.IntervalArray):
return shares_memory(left._left, right) or shares_memory(left._right, right)

if isinstance(left, ExtensionArray) and left.dtype == "string[pyarrow]":
if (
isinstance(left, ExtensionArray)
and is_string_dtype(left.dtype)
and left.dtype.storage in ("pyarrow", "pyarrow_numpy") # type: ignore[attr-defined] # noqa: E501
):
# https://github.com/pandas-dev/pandas/pull/43930#discussion_r736862669
left = cast("ArrowExtensionArray", left)
if isinstance(right, ExtensionArray) and right.dtype == "string[pyarrow]":
if (
isinstance(right, ExtensionArray)
and is_string_dtype(right.dtype)
and right.dtype.storage in ("pyarrow", "pyarrow_numpy") # type: ignore[attr-defined] # noqa: E501
):
right = cast("ArrowExtensionArray", right)
left_pa_data = left._pa_array
right_pa_data = right._pa_array
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/util/test_shares_memory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pandas.util._test_decorators as td

import pandas as pd
import pandas._testing as tm

Expand All @@ -11,3 +13,18 @@ def test_shares_memory_interval():
assert tm.shares_memory(obj, obj[:2])

assert not tm.shares_memory(obj, obj._data.copy())


@td.skip_if_no("pyarrow")
def test_shares_memory_string():
# GH#55823
import pyarrow as pa

obj = pd.array(["a", "b"], dtype="string[pyarrow]")
assert tm.shares_memory(obj, obj)

obj = pd.array(["a", "b"], dtype="string[pyarrow_numpy]")
assert tm.shares_memory(obj, obj)

obj = pd.array(["a", "b"], dtype=pd.ArrowDtype(pa.string()))
assert tm.shares_memory(obj, obj)
Loading