Skip to content

Commit c5fb994

Browse files
authored
CLN: trim unreachable indexing code (#31768)
1 parent b42505e commit c5fb994

File tree

5 files changed

+23
-48
lines changed

5 files changed

+23
-48
lines changed

pandas/core/frame.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -3011,17 +3011,12 @@ def _set_value(self, index, col, value, takeable: bool = False):
30113011
col : column label
30123012
value : scalar
30133013
takeable : interpret the index/col as indexers, default False
3014-
3015-
Returns
3016-
-------
3017-
DataFrame
3018-
If label pair is contained, will be reference to calling DataFrame,
3019-
otherwise a new object.
30203014
"""
30213015
try:
30223016
if takeable is True:
30233017
series = self._iget_item_cache(col)
3024-
return series._set_value(index, value, takeable=True)
3018+
series._set_value(index, value, takeable=True)
3019+
return
30253020

30263021
series = self._get_item_cache(col)
30273022
engine = self.index._engine
@@ -3031,7 +3026,6 @@ def _set_value(self, index, col, value, takeable: bool = False):
30313026
series._values[loc] = value
30323027
# Note: trying to use series._set_value breaks tests in
30333028
# tests.frame.indexing.test_indexing and tests.indexing.test_partial
3034-
return self
30353029
except (KeyError, TypeError):
30363030
# set using a non-recursive method & reset the cache
30373031
if takeable:
@@ -3040,8 +3034,6 @@ def _set_value(self, index, col, value, takeable: bool = False):
30403034
self.loc[index, col] = value
30413035
self._item_cache.pop(col, None)
30423036

3043-
return self
3044-
30453037
def _ensure_valid_index(self, value):
30463038
"""
30473039
Ensure that if we don't have an index, that we can create one from the

pandas/core/series.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ def _get_with(self, key):
905905
return self._get_values(key)
906906
raise
907907

908-
if not isinstance(key, (list, np.ndarray, Series, Index)):
908+
if not isinstance(key, (list, np.ndarray, ExtensionArray, Series, Index)):
909909
key = list(key)
910910

911911
if isinstance(key, Index):
@@ -1004,8 +1004,6 @@ def __setitem__(self, key, value):
10041004

10051005
try:
10061006
self._set_with_engine(key, value)
1007-
except com.SettingWithCopyError:
1008-
raise
10091007
except (KeyError, ValueError):
10101008
values = self._values
10111009
if is_integer(key) and not self.index.inferred_type == "integer":
@@ -1014,9 +1012,6 @@ def __setitem__(self, key, value):
10141012
self[:] = value
10151013
else:
10161014
self.loc[key] = value
1017-
except InvalidIndexError:
1018-
# e.g. slice
1019-
self._set_with(key, value)
10201015

10211016
except TypeError as e:
10221017
if isinstance(key, tuple) and not isinstance(self.index, MultiIndex):
@@ -1087,7 +1082,7 @@ def _set_with(self, key, value):
10871082

10881083
def _set_labels(self, key, value):
10891084
key = com.asarray_tuplesafe(key)
1090-
indexer = self.index.get_indexer(key)
1085+
indexer: np.ndarray = self.index.get_indexer(key)
10911086
mask = indexer == -1
10921087
if mask.any():
10931088
raise ValueError(f"{key[mask]} not contained in the index")
@@ -1113,12 +1108,6 @@ def _set_value(self, label, value, takeable: bool = False):
11131108
value : object
11141109
Scalar value.
11151110
takeable : interpret the index as indexers, default False
1116-
1117-
Returns
1118-
-------
1119-
Series
1120-
If label is contained, will be reference to calling Series,
1121-
otherwise a new object.
11221111
"""
11231112
try:
11241113
if takeable:
@@ -1132,8 +1121,6 @@ def _set_value(self, label, value, takeable: bool = False):
11321121
# set using a non-recursive method
11331122
self.loc[label] = value
11341123

1135-
return self
1136-
11371124
# ----------------------------------------------------------------------
11381125
# Unsorted
11391126

pandas/tests/frame/indexing/test_indexing.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1377,28 +1377,28 @@ def test_set_value(self, float_frame):
13771377
def test_set_value_resize(self, float_frame):
13781378

13791379
res = float_frame._set_value("foobar", "B", 0)
1380-
assert res is float_frame
1381-
assert res.index[-1] == "foobar"
1382-
assert res._get_value("foobar", "B") == 0
1380+
assert res is None
1381+
assert float_frame.index[-1] == "foobar"
1382+
assert float_frame._get_value("foobar", "B") == 0
13831383

13841384
float_frame.loc["foobar", "qux"] = 0
13851385
assert float_frame._get_value("foobar", "qux") == 0
13861386

13871387
res = float_frame.copy()
1388-
res3 = res._set_value("foobar", "baz", "sam")
1389-
assert res3["baz"].dtype == np.object_
1388+
res._set_value("foobar", "baz", "sam")
1389+
assert res["baz"].dtype == np.object_
13901390

13911391
res = float_frame.copy()
1392-
res3 = res._set_value("foobar", "baz", True)
1393-
assert res3["baz"].dtype == np.object_
1392+
res._set_value("foobar", "baz", True)
1393+
assert res["baz"].dtype == np.object_
13941394

13951395
res = float_frame.copy()
1396-
res3 = res._set_value("foobar", "baz", 5)
1397-
assert is_float_dtype(res3["baz"])
1398-
assert isna(res3["baz"].drop(["foobar"])).all()
1396+
res._set_value("foobar", "baz", 5)
1397+
assert is_float_dtype(res["baz"])
1398+
assert isna(res["baz"].drop(["foobar"])).all()
13991399
msg = "could not convert string to float: 'sam'"
14001400
with pytest.raises(ValueError, match=msg):
1401-
res3._set_value("foobar", "baz", "sam")
1401+
res._set_value("foobar", "baz", "sam")
14021402

14031403
def test_set_value_with_index_dtype_change(self):
14041404
df_orig = DataFrame(np.random.randn(3, 3), index=range(3), columns=list("ABC"))

pandas/tests/series/indexing/test_datetime.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,13 @@ def test_series_set_value():
7373
dates = [datetime(2001, 1, 1), datetime(2001, 1, 2)]
7474
index = DatetimeIndex(dates)
7575

76-
s = Series(dtype=object)._set_value(dates[0], 1.0)
77-
s2 = s._set_value(dates[1], np.nan)
76+
s = Series(dtype=object)
77+
s._set_value(dates[0], 1.0)
78+
s._set_value(dates[1], np.nan)
7879

7980
expected = Series([1.0, np.nan], index=index)
8081

81-
tm.assert_series_equal(s2, expected)
82-
83-
# FIXME: dont leave commented-out
84-
# s = Series(index[:1], index[:1])
85-
# s2 = s._set_value(dates[1], index[1])
86-
# assert s2.values.dtype == 'M8[ns]'
82+
tm.assert_series_equal(s, expected)
8783

8884

8985
@pytest.mark.slow

pandas/tests/series/indexing/test_indexing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,15 @@ def test_setitem_dtypes():
375375
def test_set_value(datetime_series, string_series):
376376
idx = datetime_series.index[10]
377377
res = datetime_series._set_value(idx, 0)
378-
assert res is datetime_series
378+
assert res is None
379379
assert datetime_series[idx] == 0
380380

381381
# equiv
382382
s = string_series.copy()
383383
res = s._set_value("foobar", 0)
384-
assert res is s
385-
assert res.index[-1] == "foobar"
386-
assert res["foobar"] == 0
384+
assert res is None
385+
assert s.index[-1] == "foobar"
386+
assert s["foobar"] == 0
387387

388388
s = string_series.copy()
389389
s.loc["foobar"] = 0

0 commit comments

Comments
 (0)