Skip to content

TST/BUG (string dtype): Fix and adjust indexes string tests #59544

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 9 commits into from
Sep 9, 2024
6 changes: 5 additions & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,11 @@ def sanitize_array(
dtype = StringDtype(na_value=np.nan)
subarr = dtype.construct_array_type()._from_sequence(data, dtype=dtype)

if subarr is data and copy:
if (
subarr is data
or subarr.dtype == "str"
and subarr.dtype.storage == "python"
) and copy:
subarr = subarr.copy()

else:
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_constructor_copy(self, using_infer_string):
new_index = Index(arr, copy=True, name="name")
assert isinstance(new_index, Index)
assert new_index.name == "name"
if using_infer_string and HAS_PYARROW:
if using_infer_string:
tm.assert_extension_array_equal(
new_index.values, pd.array(arr, dtype="str")
)
Expand Down Expand Up @@ -357,6 +357,7 @@ def test_view_with_args_object_array_raises(self, index):
with pytest.raises(ValueError, match=msg):
index.view("i8")
elif index.dtype == "str" and not index.dtype.storage == "python":
# TODO(infer_string): Make the errors consistent
with pytest.raises(NotImplementedError, match="i8"):
index.view("i8")
else:
Expand Down
Loading