Skip to content

Commit b6fb905

Browse files
authored
BUG: Index(Series) makes array read only for object dtype (#57139)
1 parent a302b1b commit b6fb905

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

doc/source/whatsnew/v2.2.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Fixed regressions
2828

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

3333
.. ---------------------------------------------------------------------------
3434
.. _whatsnew_221.other:

pandas/_libs/ops.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ from pandas._libs.util cimport is_nan
2929

3030
@cython.wraparound(False)
3131
@cython.boundscheck(False)
32-
def scalar_compare(object[:] values, object val, object op) -> ndarray:
32+
def scalar_compare(ndarray[object] values, object val, object op) -> ndarray:
3333
"""
3434
Compare each element of `values` array with the scalar `val`, with
3535
the comparison operation described by `op`.

pandas/core/common.py

+2
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
236236
values = list(values)
237237
elif isinstance(values, ABCIndex):
238238
return values._values
239+
elif isinstance(values, ABCSeries):
240+
return values._values
239241

240242
if isinstance(values, list) and dtype in [np.object_, object]:
241243
return construct_1d_object_array_from_listlike(values)

pandas/tests/indexes/base_class/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,10 @@ def test_inference_on_pandas_objects(self):
7171
with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
7272
result = Index(ser)
7373
assert result.dtype != np.object_
74+
75+
def test_constructor_not_read_only(self):
76+
# GH#57130
77+
ser = Series([1, 2], dtype=object)
78+
with pd.option_context("mode.copy_on_write", True):
79+
idx = Index(ser)
80+
assert idx._values.flags.writeable

pandas/tests/indexes/test_common.py

+9
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,12 @@ def test_ndarray_compat_properties(index):
500500
# test for validity
501501
idx.nbytes
502502
idx.values.nbytes
503+
504+
505+
def test_compare_read_only_array():
506+
# GH#57130
507+
arr = np.array([], dtype=object)
508+
arr.flags.writeable = False
509+
idx = pd.Index(arr)
510+
result = idx > 69
511+
assert result.dtype == bool

0 commit comments

Comments
 (0)