From bf6c5c3c7b466d211df663ae11e3c027b85982bc Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Thu, 3 Oct 2019 07:56:10 +0100 Subject: [PATCH] DEPR: Deprecate Index.set_value (#28621) --- doc/source/reference/indexing.rst | 1 - doc/source/whatsnew/v1.0.0.rst | 4 +++- pandas/core/indexes/base.py | 14 +++++++++++++- pandas/tests/indexes/test_base.py | 13 +++++++++---- 4 files changed, 25 insertions(+), 7 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 2668734031ee1..16d23d675a8bb 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -123,7 +123,9 @@ Documentation Improvements Deprecations ~~~~~~~~~~~~ -- +- ``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`). - .. _whatsnew_1000.prior_deprecations: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 0b5f9fb61fce8..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 @@ -4680,10 +4682,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..82d5ddd1ac358 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1908,16 +1908,21 @@ 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): + # GH 28621 + 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",