Skip to content

[WIP] Make Index.values read-only #33055

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

Closed
wants to merge 5 commits into from
Closed
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 ci/deps/azure-36-32bit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ dependencies:
# see comment above
- pip
- pip:
- cython>=0.29.16
- Cython==3.0a5
- pytest>=5.0.1,<6.0.0rc0
4 changes: 3 additions & 1 deletion ci/deps/azure-36-locale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.6.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- pytest-asyncio
Expand Down Expand Up @@ -36,3 +35,6 @@ dependencies:
- xlsxwriter
- xlwt
- moto

- pip:
- Cython==3.0a5
2 changes: 1 addition & 1 deletion ci/deps/azure-36-locale_slow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.6.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -29,4 +28,5 @@ dependencies:
- xlwt=1.2.0
- pip
- pip:
- Cython==3.0a5
- html5lib==1.0b2
4 changes: 3 additions & 1 deletion ci/deps/azure-36-minimum_versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ dependencies:
- python=3.6.1

# tools
- cython=0.29.16
- pytest=5.0.1
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -28,3 +27,6 @@ dependencies:
- xlsxwriter=0.9.8
- xlwt=1.2.0
- html5lib=1.0.1

- pip:
- Cython==3.0a5
3 changes: 2 additions & 1 deletion ci/deps/azure-36-slow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.6.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -33,3 +32,5 @@ dependencies:
- xlsxwriter
- xlwt
- moto
- pip:
- Cython==3.0a5
2 changes: 1 addition & 1 deletion ci/deps/azure-37-locale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ dependencies:
- python=3.7.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- pytest-asyncio
Expand Down Expand Up @@ -35,4 +34,5 @@ dependencies:
- pyarrow>=0.15
- pip
- pip:
- Cython==3.0a5
- pyxlsb
2 changes: 1 addition & 1 deletion ci/deps/azure-37-numpydev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
- pytz
- pip
- pip:
- cython==0.29.16 # GH#34014
- Cython==3.0a5
- "git+git://github.com/dateutil/dateutil.git"
- "--extra-index-url https://pypi.anaconda.org/scipy-wheels-nightly/simple"
- "--pre"
Expand Down
2 changes: 1 addition & 1 deletion ci/deps/azure-macos-36.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ dependencies:
- xlwt
- pip
- pip:
- cython>=0.29.16
- Cython==3.0a5
- pyreadstat
- pyxlsb
3 changes: 2 additions & 1 deletion ci/deps/azure-windows-36.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.6.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -30,3 +29,5 @@ dependencies:
- xlrd
- xlsxwriter
- xlwt
- pip:
- Cython==3.0a5
2 changes: 1 addition & 1 deletion ci/deps/azure-windows-37.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.7.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand Down Expand Up @@ -38,4 +37,5 @@ dependencies:
- pyreadstat
- pip
- pip:
- Cython==3.0a5
- pyxlsb
3 changes: 1 addition & 2 deletions ci/deps/travis-36-cov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.6.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -15,7 +14,6 @@ dependencies:
# pandas dependencies
- beautifulsoup4
- botocore>=1.11
- cython>=0.29.16
- dask
- fastparquet>=0.3.2
- fsspec>=0.7.4
Expand Down Expand Up @@ -47,6 +45,7 @@ dependencies:
- xlwt
- pip
- pip:
- Cython==3.0a5
- brotlipy
- coverage
- pandas-datareader
Expand Down
3 changes: 2 additions & 1 deletion ci/deps/travis-36-locale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.6.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand Down Expand Up @@ -38,3 +37,5 @@ dependencies:
- xlrd
- xlsxwriter
- xlwt
- pip:
- Cython==3.0a5
2 changes: 1 addition & 1 deletion ci/deps/travis-37-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.7.*

# tools
- cython>=0.29.13
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -18,4 +17,5 @@ dependencies:
- pytz
- pip
- pip:
- Cython==3.0a5
- moto
2 changes: 1 addition & 1 deletion ci/deps/travis-37.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.7.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -24,4 +23,5 @@ dependencies:
- pyreadstat
- pip
- pip:
- Cython==3.0a5
- moto
3 changes: 2 additions & 1 deletion ci/deps/travis-38.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- python=3.8.*

# tools
- cython>=0.29.16
- pytest>=5.0.1,<6.0.0rc0
- pytest-xdist>=1.21
- hypothesis>=3.58.0
Expand All @@ -18,3 +17,5 @@ dependencies:
- pytz
- pip
- tabulate==0.8.3
- pip:
- Cython==3.0a5
12 changes: 6 additions & 6 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@ def __setitem__(self, key, value):

# Need to ensure that left and right are updated atomically, so we're
# forced to copy, update the copy, and swap in the new values.
left = self.left.copy(deep=True)
left._values[key] = value_left
self._left = left
left_data = self.left._data.copy()
left_data[key] = value_left
self._left = self.left._shallow_copy(left_data)

right = self.right.copy(deep=True)
right._values[key] = value_right
self._right = right
right_data = self.right._data.copy()
right_data[key] = value_right
self._right = self.right._shallow_copy(right_data)

def __eq__(self, other):
# ensure pandas array for list-like and eliminate non-interval scalars
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3849,7 +3849,9 @@ def values(self) -> np.ndarray:
Index.array : Reference to the underlying data.
Index.to_numpy : A NumPy array representing the underlying data.
"""
return self._data.view(np.ndarray)
vals = self._data.view(np.ndarray)
vals.setflags(write=False)
return vals

@cache_readonly
@doc(IndexOpsMixin.array)
Expand All @@ -3858,7 +3860,7 @@ def array(self) -> ExtensionArray:
if isinstance(array, np.ndarray):
from pandas.core.arrays.numpy_ import PandasArray

array = PandasArray(array)
array = PandasArray(self.values)
return array

@property
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def test_mutability(index):
index[0] = index[0]


def test_values_mutability(index):
if not len(index):
return
Copy link
Member

Choose a reason for hiding this comment

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

moot point till tests are passing, but ideally should use pytest.skip with message.

with pytest.raises(ValueError):
index.values[0] = index[0]


def test_array_mutability(index):
if not len(index):
return
with pytest.raises(ValueError):
index.array[0] = index[0]


def test_wrong_number_names(index):
names = index.nlevels * ["apple", "banana", "carrot"]
with pytest.raises(ValueError, match="^Length"):
Expand Down