Skip to content

Commit

Permalink
Backport PR pandas-dev#57139: BUG: Index(Series) makes array read onl…
Browse files Browse the repository at this point in the history
…y for object dtype
  • Loading branch information
phofl authored and meeseeksmachine committed Jan 30, 2024
1 parent 10b5873 commit 5b2549d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
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

0 comments on commit 5b2549d

Please sign in to comment.