Skip to content

Commit bf6c5c3

Browse files
topper-123jorisvandenbossche
authored andcommitted
DEPR: Deprecate Index.set_value (#28621)
1 parent ef936f9 commit bf6c5c3

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

doc/source/reference/indexing.rst

-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ Selecting
166166
Index.get_slice_bound
167167
Index.get_value
168168
Index.get_values
169-
Index.set_value
170169
Index.isin
171170
Index.slice_indexer
172171
Index.slice_locs

doc/source/whatsnew/v1.0.0.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ Documentation Improvements
123123
Deprecations
124124
~~~~~~~~~~~~
125125

126-
-
126+
- ``Index.set_value`` has been deprecated. For a given index ``idx``, array ``arr``,
127+
value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)``
128+
is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`).
127129
-
128130

129131
.. _whatsnew_1000.prior_deprecations:

pandas/core/indexes/base.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ class Index(IndexOpsMixin, PandasObject):
205205
"""
206206

207207
# tolist is not actually deprecated, just suppressed in the __dir__
208-
_deprecations = DirNamesMixin._deprecations | frozenset(["tolist", "dtype_str"])
208+
_deprecations = DirNamesMixin._deprecations | frozenset(
209+
["tolist", "dtype_str", "set_value"]
210+
)
209211

210212
# To hand over control to subclasses
211213
_join_precedence = 1
@@ -4680,10 +4682,20 @@ def set_value(self, arr, key, value):
46804682
"""
46814683
Fast lookup of value from 1-dimensional ndarray.
46824684
4685+
.. deprecated:: 1.0
4686+
46834687
Notes
46844688
-----
46854689
Only use this if you know what you're doing.
46864690
"""
4691+
warnings.warn(
4692+
(
4693+
"The 'set_value' method is deprecated, and "
4694+
"will be removed in a future version."
4695+
),
4696+
FutureWarning,
4697+
stacklevel=2,
4698+
)
46874699
self._engine.set_value(
46884700
com.values_from_object(arr), com.values_from_object(key), value
46894701
)

pandas/tests/indexes/test_base.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1908,16 +1908,21 @@ def test_is_monotonic_incomparable(self, attr):
19081908
index = Index([5, datetime.now(), 7])
19091909
assert not getattr(index, attr)
19101910

1911-
def test_get_set_value(self):
1911+
def test_set_value_deprecated(self):
1912+
# GH 28621
1913+
idx = self.create_index()
1914+
arr = np.array([1, 2, 3])
1915+
with tm.assert_produces_warning(FutureWarning):
1916+
idx.set_value(arr, idx[1], 80)
1917+
assert arr[1] == 80
1918+
1919+
def test_get_value(self):
19121920
# TODO: Remove function? GH 19728
19131921
values = np.random.randn(100)
19141922
date = self.dateIndex[67]
19151923

19161924
assert_almost_equal(self.dateIndex.get_value(values, date), values[67])
19171925

1918-
self.dateIndex.set_value(values, date, 10)
1919-
assert values[67] == 10
1920-
19211926
@pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}])
19221927
@pytest.mark.parametrize(
19231928
"index,expected",

0 commit comments

Comments
 (0)