Skip to content

Commit 000245a

Browse files
authored
CLN: remove IndexEngine.set_value (#31510)
1 parent db16a8f commit 000245a

File tree

5 files changed

+31
-42
lines changed

5 files changed

+31
-42
lines changed

pandas/_libs/index.pyx

+13-20
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,6 @@ cdef class IndexEngine:
8787
else:
8888
return get_value_at(arr, loc, tz=tz)
8989

90-
cpdef set_value(self, ndarray arr, object key, object value):
91-
"""
92-
Parameters
93-
----------
94-
arr : 1-dimensional ndarray
95-
"""
96-
cdef:
97-
object loc
98-
99-
loc = self.get_loc(key)
100-
value = convert_scalar(arr, value)
101-
102-
arr[loc] = value
103-
10490
cpdef get_loc(self, object val):
10591
cdef:
10692
Py_ssize_t loc
@@ -585,16 +571,23 @@ cpdef convert_scalar(ndarray arr, object value):
585571
raise ValueError("cannot set a Timedelta with a non-timedelta "
586572
f"{type(value).__name__}")
587573

588-
if (issubclass(arr.dtype.type, (np.integer, np.floating, np.complex)) and
589-
not issubclass(arr.dtype.type, np.bool_)):
590-
if util.is_bool_object(value):
591-
raise ValueError("Cannot assign bool to float/integer series")
574+
else:
575+
validate_numeric_casting(arr.dtype, value)
576+
577+
return value
578+
592579

593-
if issubclass(arr.dtype.type, (np.integer, np.bool_)):
580+
cpdef validate_numeric_casting(dtype, object value):
581+
# Note: we can't annotate dtype as cnp.dtype because that cases dtype.type
582+
# to integer
583+
if issubclass(dtype.type, (np.integer, np.bool_)):
594584
if util.is_float_object(value) and value != value:
595585
raise ValueError("Cannot assign nan to integer series")
596586

597-
return value
587+
if (issubclass(dtype.type, (np.integer, np.floating, np.complex)) and
588+
not issubclass(dtype.type, np.bool_)):
589+
if util.is_bool_object(value):
590+
raise ValueError("Cannot assign bool to float/integer series")
598591

599592

600593
cdef class BaseMultiIndexCodesEngine:

pandas/core/frame.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
from pandas._config import get_option
4242

43-
from pandas._libs import algos as libalgos, lib, properties
43+
from pandas._libs import algos as libalgos, index as libindex, lib, properties
4444
from pandas._typing import Axes, Axis, Dtype, FilePathOrBuffer, Label, Level, Renamer
4545
from pandas.compat import PY37
4646
from pandas.compat._optional import import_optional_dependency
@@ -3028,10 +3028,14 @@ def _set_value(self, index, col, value, takeable: bool = False):
30283028

30293029
series = self._get_item_cache(col)
30303030
engine = self.index._engine
3031-
engine.set_value(series._values, index, value)
3031+
loc = engine.get_loc(index)
3032+
libindex.validate_numeric_casting(series.dtype, value)
3033+
3034+
series._values[loc] = value
3035+
# Note: trying to use series._set_value breaks tests in
3036+
# tests.frame.indexing.test_indexing and tests.indexing.test_partial
30323037
return self
30333038
except (KeyError, TypeError):
3034-
30353039
# set using a non-recursive method & reset the cache
30363040
if takeable:
30373041
self.iloc[index, col] = value

pandas/core/indexes/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4650,9 +4650,9 @@ def set_value(self, arr, key, value):
46504650
FutureWarning,
46514651
stacklevel=2,
46524652
)
4653-
self._engine.set_value(
4654-
com.values_from_object(arr), com.values_from_object(key), value
4655-
)
4653+
loc = self._engine.get_loc(key)
4654+
libindex.validate_numeric_casting(arr.dtype, value)
4655+
arr[loc] = value
46564656

46574657
_index_shared_docs[
46584658
"get_indexer_non_unique"

pandas/core/series.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -1026,17 +1026,10 @@ def __setitem__(self, key, value):
10261026
self._maybe_update_cacher()
10271027

10281028
def _set_with_engine(self, key, value):
1029-
values = self._values
1030-
if is_extension_array_dtype(values.dtype):
1031-
# The cython indexing engine does not support ExtensionArrays.
1032-
values[self.index.get_loc(key)] = value
1033-
return
1034-
try:
1035-
self.index._engine.set_value(values, key, value)
1036-
return
1037-
except KeyError:
1038-
values[self.index.get_loc(key)] = value
1039-
return
1029+
# fails with AttributeError for IntervalIndex
1030+
loc = self.index._engine.get_loc(key)
1031+
libindex.validate_numeric_casting(self.dtype, value)
1032+
self._values[loc] = value
10401033

10411034
def _set_with(self, key, value):
10421035
# other: fancy integer or otherwise
@@ -1116,11 +1109,10 @@ def _set_value(self, label, value, takeable: bool = False):
11161109
try:
11171110
if takeable:
11181111
self._values[label] = value
1119-
elif isinstance(self._values, np.ndarray):
1120-
# i.e. not EA, so we can use _engine
1121-
self.index._engine.set_value(self._values, label, value)
11221112
else:
1123-
self.loc[label] = value
1113+
loc = self.index.get_loc(label)
1114+
libindex.validate_numeric_casting(self.dtype, value)
1115+
self._values[loc] = value
11241116
except KeyError:
11251117

11261118
# set using a non-recursive method

pandas/tests/indexing/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def test_setitem_ndarray_3d(self, index, obj, idxr, idxr_id):
137137
r"Buffer has wrong number of dimensions \(expected 1, "
138138
r"got 3\)|"
139139
"'pandas._libs.interval.IntervalTree' object has no attribute "
140-
"'set_value'|" # AttributeError
140+
"'get_loc'|" # AttributeError
141141
"unhashable type: 'numpy.ndarray'|" # TypeError
142142
"No matching signature found|" # TypeError
143143
r"^\[\[\[|" # pandas.core.indexing.IndexingError

0 commit comments

Comments
 (0)