From 9a41b7253f095f7258379630c445dde85aed3c4d Mon Sep 17 00:00:00 2001 From: tp Date: Wed, 25 Sep 2019 20:28:19 +0100 Subject: [PATCH 1/4] DEPR: Deprecate Index.set_value --- doc/source/whatsnew/v1.0.0.rst | 4 +++- pandas/core/indexes/base.py | 10 ++++++++++ pandas/tests/indexes/test_base.py | 12 ++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 2668734031ee1..8117bc60051ad 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -123,7 +123,9 @@ Documentation Improvements Deprecations ~~~~~~~~~~~~ -- +- :meth:`Index.set_values` has been deprecated. For a given index ``idx``, array ``arr``, + value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)`` + is equivalent to ``arr[idx.get_loc(idx_val) = val``, which should be used instead (:issue:`28621`). - .. _whatsnew_1000.prior_deprecations: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0b5f9fb61fce8..15496958b21b2 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4680,10 +4680,20 @@ def set_value(self, arr, key, value): """ Fast lookup of value from 1-dimensional ndarray. + .. deprecated:: 1.0 + Notes ----- Only use this if you know what you're doing. """ + warnings.warn( + ( + "The 'set_value' method is deprecated, and " + "will be removed in a future version." + ), + FutureWarning, + stacklevel=2, + ) self._engine.set_value( com.values_from_object(arr), com.values_from_object(key), value ) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index d1ed79118d2fa..f753e1edb63ae 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1908,16 +1908,20 @@ def test_is_monotonic_incomparable(self, attr): index = Index([5, datetime.now(), 7]) assert not getattr(index, attr) - def test_get_set_value(self): + def test_set_value_deprecated(self): + idx = self.create_index() + arr = np.array([1, 2, 3]) + with tm.assert_produces_warning(FutureWarning): + idx.set_value(arr, idx[1], 80) + assert arr[1] == 80 + + def test_get_value(self): # TODO: Remove function? GH 19728 values = np.random.randn(100) date = self.dateIndex[67] assert_almost_equal(self.dateIndex.get_value(values, date), values[67]) - self.dateIndex.set_value(values, date, 10) - assert values[67] == 10 - @pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}]) @pytest.mark.parametrize( "index,expected", From e790a591fa3718fc172b6ecd46e979956fadcfa4 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 26 Sep 2019 17:34:27 +0100 Subject: [PATCH 2/4] Add github issue to test --- pandas/tests/indexes/test_base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index f753e1edb63ae..82d5ddd1ac358 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1909,6 +1909,7 @@ def test_is_monotonic_incomparable(self, attr): assert not getattr(index, attr) def test_set_value_deprecated(self): + # GH 28621 idx = self.create_index() arr = np.array([1, 2, 3]) with tm.assert_produces_warning(FutureWarning): From b708d9292f0fd724ceb752600909ae46e2e817ed Mon Sep 17 00:00:00 2001 From: tp Date: Wed, 2 Oct 2019 22:38:27 +0100 Subject: [PATCH 3/4] change according to comments --- doc/source/reference/indexing.rst | 1 - doc/source/whatsnew/v1.0.0.rst | 4 ++-- pandas/core/indexes/base.py | 4 +++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/source/reference/indexing.rst b/doc/source/reference/indexing.rst index 576f734d517aa..dd59a99b3df9e 100644 --- a/doc/source/reference/indexing.rst +++ b/doc/source/reference/indexing.rst @@ -166,7 +166,6 @@ Selecting Index.get_slice_bound Index.get_value Index.get_values - Index.set_value Index.isin Index.slice_indexer Index.slice_locs diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 8117bc60051ad..7f897545a1fa1 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -123,9 +123,9 @@ Documentation Improvements Deprecations ~~~~~~~~~~~~ -- :meth:`Index.set_values` has been deprecated. For a given index ``idx``, array ``arr``, +- `Index.set_value` has been deprecated. For a given index ``idx``, array ``arr``, value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)`` - is equivalent to ``arr[idx.get_loc(idx_val) = val``, which should be used instead (:issue:`28621`). + is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`). - .. _whatsnew_1000.prior_deprecations: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 15496958b21b2..afa4f1a5a8c76 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -205,7 +205,9 @@ class Index(IndexOpsMixin, PandasObject): """ # tolist is not actually deprecated, just suppressed in the __dir__ - _deprecations = DirNamesMixin._deprecations | frozenset(["tolist", "dtype_str"]) + _deprecations = DirNamesMixin._deprecations | frozenset( + ["tolist", "dtype_str", "set_value"] + ) # To hand over control to subclasses _join_precedence = 1 From 2f422bba3a5c332d86fbaac70979252fe945704b Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 3 Oct 2019 08:55:25 +0200 Subject: [PATCH 4/4] fix quoting --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 7f897545a1fa1..16d23d675a8bb 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -123,7 +123,7 @@ Documentation Improvements Deprecations ~~~~~~~~~~~~ -- `Index.set_value` has been deprecated. For a given index ``idx``, array ``arr``, +- ``Index.set_value`` has been deprecated. For a given index ``idx``, array ``arr``, value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)`` is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`). -