Skip to content
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
5 changes: 3 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5023,8 +5023,9 @@ def array(self) -> ExtensionArray:
from pandas.core.arrays.numpy_ import NumpyExtensionArray

array = NumpyExtensionArray(array)
array = array.view()
array._readonly = True
# TODO decide on read-only https://github.com/pandas-dev/pandas/issues/63099
# array = array.view()
# array._readonly = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the idea to leave this commented-out indefinitely? or will be cleaned up before merging?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id prefer to remove but not going to make a stink about it. merge on green(ish)

return array

@property
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2387,7 +2387,9 @@ def external_values(values: ArrayLike) -> ArrayLike:
values.flags.writeable = False
else:
# ExtensionArrays
values = values.view()
values._readonly = True
# TODO decide on read-only https://github.com/pandas-dev/pandas/issues/63099
# values = values.view()
# values._readonly = True
pass

return values
5 changes: 3 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,9 @@ def _references(self) -> BlockValuesRefs:
@property
def array(self) -> ExtensionArray:
arr = self._mgr.array_values()
arr = arr.view()
arr._readonly = True
# TODO decide on read-only https://github.com/pandas-dev/pandas/issues/63099
# arr = arr.view()
# arr._readonly = True
return arr

def __len__(self) -> int:
Expand Down
37 changes: 34 additions & 3 deletions pandas/tests/copy_view/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@
],
ids=["values", "array", "np.asarray", "np.array"],
)
def test_series_values(method):
def test_series_values(request, method):
ser = Series([1, 2, 3], name="name")
ser_orig = ser.copy()

arr = method(ser)

if request.node.callspec.id == "array":
# https://github.com/pandas-dev/pandas/issues/63099
# .array for now does not return a read-only view
assert arr.flags.writeable is True
# updating the array updates the series
arr[0] = 0
assert ser.iloc[0] == 0
return

# .values still gives a view but is read-only
assert np.shares_memory(arr, get_array(ser, "name"))
assert arr.flags.writeable is False
Expand Down Expand Up @@ -109,20 +118,42 @@ def test_series_to_numpy():
@pytest.mark.parametrize(
"method",
[
lambda ser: np.asarray(ser.values),
lambda ser: np.asarray(ser.array),
lambda ser: np.asarray(ser),
lambda ser: np.asarray(ser, dtype="int64"),
lambda ser: np.array(ser, copy=False),
],
ids=["array", "np.asarray", "np.asarray-dtype", "np.array"],
ids=["values", "array", "np.asarray", "np.asarray-dtype", "np.array"],
)
def test_series_values_ea_dtypes(method):
def test_series_values_ea_dtypes(request, method):
ser = Series([1, 2, 3], dtype="Int64")
ser_orig = ser.copy()

arr = method(ser)

if request.node.callspec.id in ("values", "array"):
# https://github.com/pandas-dev/pandas/issues/63099
# .array/values for now does not return a read-only view
assert arr.flags.writeable is True
# updating the array updates the series
arr[0] = 0
assert ser.iloc[0] == 0
return

# conversion to ndarray gives a view but is read-only
assert np.shares_memory(arr, get_array(ser))
assert arr.flags.writeable is False

# mutating series through arr therefore doesn't work
with pytest.raises(ValueError, match="read-only"):
arr[0] = 0
tm.assert_series_equal(ser, ser_orig)

# mutating the series itself still works
ser.iloc[0] = 0
assert ser.values[0] == 0


@pytest.mark.parametrize(
"method",
Expand Down
Loading