Skip to content
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

DEPR: Deprecate Index.set_value #28621

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion doc/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 13 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
13 changes: 9 additions & 4 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down