From 5489d5161f19ec5031dbbd9f3c09b863ba356b32 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Fri, 27 Mar 2020 13:53:23 +1100 Subject: [PATCH 1/5] Make Index.values read-only --- pandas/core/indexes/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 986d6323e704e..2d4eabc4da864 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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) From 82c260cfa0c8c2c230e5dbbc5ccba996ae4884a1 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Fri, 27 Mar 2020 14:45:31 +1100 Subject: [PATCH 2/5] Fix for interval arrays --- pandas/core/arrays/interval.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index c861d25afd13f..2f3773671e24f 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -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 From 48146ade0e199c2c599f7cdd10facd8e71e912e7 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Sat, 28 Mar 2020 15:58:24 +1100 Subject: [PATCH 3/5] Simple idx.array immutability --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2d4eabc4da864..6496a47af318e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3860,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 From ad4f563a0945dc3c96407f76bcbfaca1b0d03238 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Sat, 28 Mar 2020 15:58:56 +1100 Subject: [PATCH 4/5] Tests for idx.array and idx.values immutability --- pandas/tests/indexes/test_any_index.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/indexes/test_any_index.py b/pandas/tests/indexes/test_any_index.py index 5e7065f785309..5ce72ae153c49 100644 --- a/pandas/tests/indexes/test_any_index.py +++ b/pandas/tests/indexes/test_any_index.py @@ -33,6 +33,20 @@ def test_mutability(index): index[0] = index[0] +def test_values_mutability(index): + if not len(index): + return + 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"): From e1fda5b26be8d5d58a2bde8ca83d7443a9518c86 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Wed, 22 Jul 2020 01:22:28 +1000 Subject: [PATCH 5/5] Use latest cython on CI --- ci/deps/azure-36-32bit.yaml | 2 +- ci/deps/azure-36-locale.yaml | 4 +++- ci/deps/azure-36-locale_slow.yaml | 2 +- ci/deps/azure-36-minimum_versions.yaml | 4 +++- ci/deps/azure-36-slow.yaml | 3 ++- ci/deps/azure-37-locale.yaml | 2 +- ci/deps/azure-37-numpydev.yaml | 2 +- ci/deps/azure-macos-36.yaml | 2 +- ci/deps/azure-windows-36.yaml | 3 ++- ci/deps/azure-windows-37.yaml | 2 +- ci/deps/travis-36-cov.yaml | 3 +-- ci/deps/travis-36-locale.yaml | 3 ++- ci/deps/travis-37-arm64.yaml | 2 +- ci/deps/travis-37.yaml | 2 +- ci/deps/travis-38.yaml | 3 ++- 15 files changed, 23 insertions(+), 16 deletions(-) diff --git a/ci/deps/azure-36-32bit.yaml b/ci/deps/azure-36-32bit.yaml index 2dc53f8181ac4..3a661beb34f48 100644 --- a/ci/deps/azure-36-32bit.yaml +++ b/ci/deps/azure-36-32bit.yaml @@ -22,5 +22,5 @@ dependencies: # see comment above - pip - pip: - - cython>=0.29.16 + - Cython==3.0a5 - pytest>=5.0.1,<6.0.0rc0 diff --git a/ci/deps/azure-36-locale.yaml b/ci/deps/azure-36-locale.yaml index d31015fde4741..9c9b861b67a1c 100644 --- a/ci/deps/azure-36-locale.yaml +++ b/ci/deps/azure-36-locale.yaml @@ -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 @@ -36,3 +35,6 @@ dependencies: - xlsxwriter - xlwt - moto + + - pip: + - Cython==3.0a5 diff --git a/ci/deps/azure-36-locale_slow.yaml b/ci/deps/azure-36-locale_slow.yaml index 23121b985492e..75710dfc301b3 100644 --- a/ci/deps/azure-36-locale_slow.yaml +++ b/ci/deps/azure-36-locale_slow.yaml @@ -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 @@ -29,4 +28,5 @@ dependencies: - xlwt=1.2.0 - pip - pip: + - Cython==3.0a5 - html5lib==1.0b2 diff --git a/ci/deps/azure-36-minimum_versions.yaml b/ci/deps/azure-36-minimum_versions.yaml index f5af7bcf36189..e8183ee579a34 100644 --- a/ci/deps/azure-36-minimum_versions.yaml +++ b/ci/deps/azure-36-minimum_versions.yaml @@ -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 @@ -28,3 +27,6 @@ dependencies: - xlsxwriter=0.9.8 - xlwt=1.2.0 - html5lib=1.0.1 + + - pip: + - Cython==3.0a5 diff --git a/ci/deps/azure-36-slow.yaml b/ci/deps/azure-36-slow.yaml index 0a6d1d13c8549..ae1aa0143b4fc 100644 --- a/ci/deps/azure-36-slow.yaml +++ b/ci/deps/azure-36-slow.yaml @@ -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 @@ -33,3 +32,5 @@ dependencies: - xlsxwriter - xlwt - moto + - pip: + - Cython==3.0a5 diff --git a/ci/deps/azure-37-locale.yaml b/ci/deps/azure-37-locale.yaml index 77aae791a47c1..6c3452d43d8f3 100644 --- a/ci/deps/azure-37-locale.yaml +++ b/ci/deps/azure-37-locale.yaml @@ -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 @@ -35,4 +34,5 @@ dependencies: - pyarrow>=0.15 - pip - pip: + - Cython==3.0a5 - pyxlsb diff --git a/ci/deps/azure-37-numpydev.yaml b/ci/deps/azure-37-numpydev.yaml index 451fb5884a4af..87bdf8825e5d3 100644 --- a/ci/deps/azure-37-numpydev.yaml +++ b/ci/deps/azure-37-numpydev.yaml @@ -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" diff --git a/ci/deps/azure-macos-36.yaml b/ci/deps/azure-macos-36.yaml index 81a27465f9e61..cdbaa8fa7892a 100644 --- a/ci/deps/azure-macos-36.yaml +++ b/ci/deps/azure-macos-36.yaml @@ -31,6 +31,6 @@ dependencies: - xlwt - pip - pip: - - cython>=0.29.16 + - Cython==3.0a5 - pyreadstat - pyxlsb diff --git a/ci/deps/azure-windows-36.yaml b/ci/deps/azure-windows-36.yaml index 4d7e1d821037b..ab49a9caef77e 100644 --- a/ci/deps/azure-windows-36.yaml +++ b/ci/deps/azure-windows-36.yaml @@ -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 @@ -30,3 +29,5 @@ dependencies: - xlrd - xlsxwriter - xlwt + - pip: + - Cython==3.0a5 \ No newline at end of file diff --git a/ci/deps/azure-windows-37.yaml b/ci/deps/azure-windows-37.yaml index 34fca631df6c1..73cdb0ff1bd14 100644 --- a/ci/deps/azure-windows-37.yaml +++ b/ci/deps/azure-windows-37.yaml @@ -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 @@ -38,4 +37,5 @@ dependencies: - pyreadstat - pip - pip: + - Cython==3.0a5 - pyxlsb diff --git a/ci/deps/travis-36-cov.yaml b/ci/deps/travis-36-cov.yaml index 5f5ea8034cddf..78b721d0fc7b4 100644 --- a/ci/deps/travis-36-cov.yaml +++ b/ci/deps/travis-36-cov.yaml @@ -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 @@ -15,7 +14,6 @@ dependencies: # pandas dependencies - beautifulsoup4 - botocore>=1.11 - - cython>=0.29.16 - dask - fastparquet>=0.3.2 - fsspec>=0.7.4 @@ -47,6 +45,7 @@ dependencies: - xlwt - pip - pip: + - Cython==3.0a5 - brotlipy - coverage - pandas-datareader diff --git a/ci/deps/travis-36-locale.yaml b/ci/deps/travis-36-locale.yaml index 6bc4aba733ee5..e31a83d2b02fb 100644 --- a/ci/deps/travis-36-locale.yaml +++ b/ci/deps/travis-36-locale.yaml @@ -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 @@ -38,3 +37,5 @@ dependencies: - xlrd - xlsxwriter - xlwt + - pip: + - Cython==3.0a5 \ No newline at end of file diff --git a/ci/deps/travis-37-arm64.yaml b/ci/deps/travis-37-arm64.yaml index f434a03609b26..2f7cf2d975ed1 100644 --- a/ci/deps/travis-37-arm64.yaml +++ b/ci/deps/travis-37-arm64.yaml @@ -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 @@ -18,4 +17,5 @@ dependencies: - pytz - pip - pip: + - Cython==3.0a5 - moto diff --git a/ci/deps/travis-37.yaml b/ci/deps/travis-37.yaml index aaf706d61fe5c..ce02a546deddd 100644 --- a/ci/deps/travis-37.yaml +++ b/ci/deps/travis-37.yaml @@ -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 @@ -24,4 +23,5 @@ dependencies: - pyreadstat - pip - pip: + - Cython==3.0a5 - moto diff --git a/ci/deps/travis-38.yaml b/ci/deps/travis-38.yaml index ac39a223cd086..1f5a0ade9444d 100644 --- a/ci/deps/travis-38.yaml +++ b/ci/deps/travis-38.yaml @@ -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 @@ -18,3 +17,5 @@ dependencies: - pytz - pip - tabulate==0.8.3 + - pip: + - Cython==3.0a5 \ No newline at end of file