Skip to content

Backport PR #57139 on branch 2.2.x (BUG: Index(Series) makes array read only for object dtype) #57143

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)

.. ---------------------------------------------------------------------------
.. _whatsnew_221.other:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/ops.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ from pandas._libs.util cimport is_nan

@cython.wraparound(False)
@cython.boundscheck(False)
def scalar_compare(object[:] values, object val, object op) -> ndarray:
def scalar_compare(ndarray[object] values, object val, object op) -> ndarray:
"""
Compare each element of `values` array with the scalar `val`, with
the comparison operation described by `op`.
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
values = list(values)
elif isinstance(values, ABCIndex):
return values._values
elif isinstance(values, ABCSeries):
return values._values

if isinstance(values, list) and dtype in [np.object_, object]:
return construct_1d_object_array_from_listlike(values)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/base_class/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ def test_inference_on_pandas_objects(self):
with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
result = Index(ser)
assert result.dtype != np.object_

def test_constructor_not_read_only(self):
# GH#57130
ser = Series([1, 2], dtype=object)
with pd.option_context("mode.copy_on_write", True):
idx = Index(ser)
assert idx._values.flags.writeable
9 changes: 9 additions & 0 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,12 @@ def test_ndarray_compat_properties(index):
# test for validity
idx.nbytes
idx.values.nbytes


def test_compare_read_only_array():
# GH#57130
arr = np.array([], dtype=object)
arr.flags.writeable = False
idx = pd.Index(arr)
result = idx > 69
assert result.dtype == bool