Skip to content

Commit 67c9385

Browse files
authored
CLN: Remove raise if missing only controlling the error message (#41371)
1 parent a5f5115 commit 67c9385

File tree

10 files changed

+56
-134
lines changed

10 files changed

+56
-134
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3412,7 +3412,7 @@ def __getitem__(self, key):
34123412
else:
34133413
if is_iterator(key):
34143414
key = list(key)
3415-
indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
3415+
indexer = self.loc._get_listlike_indexer(key, axis=1)[1]
34163416

34173417
# take() does not accept boolean indexers
34183418
if getattr(indexer, "dtype", None) == bool:

pandas/core/indexing.py

+9-37
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
import numpy as np
1313

14-
from pandas._config.config import option_context
15-
1614
from pandas._libs.indexing import NDFrameIndexerBase
1715
from pandas._libs.lib import item_from_zerodim
1816
from pandas.errors import (
@@ -1089,7 +1087,7 @@ def _getitem_iterable(self, key, axis: int):
10891087
self._validate_key(key, axis)
10901088

10911089
# A collection of keys
1092-
keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)
1090+
keyarr, indexer = self._get_listlike_indexer(key, axis)
10931091
return self.obj._reindex_with_indexers(
10941092
{axis: [keyarr, indexer]}, copy=True, allow_dups=True
10951093
)
@@ -1255,8 +1253,7 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
12551253
(inds,) = key.nonzero()
12561254
return inds
12571255
else:
1258-
# When setting, missing keys are not allowed, even with .loc:
1259-
return self._get_listlike_indexer(key, axis, raise_missing=True)[1]
1256+
return self._get_listlike_indexer(key, axis)[1]
12601257
else:
12611258
try:
12621259
return labels.get_loc(key)
@@ -1266,7 +1263,7 @@ def _convert_to_indexer(self, key, axis: int, is_setter: bool = False):
12661263
return {"key": key}
12671264
raise
12681265

1269-
def _get_listlike_indexer(self, key, axis: int, raise_missing: bool = False):
1266+
def _get_listlike_indexer(self, key, axis: int):
12701267
"""
12711268
Transform a list-like of keys into a new index and an indexer.
12721269
@@ -1276,16 +1273,11 @@ def _get_listlike_indexer(self, key, axis: int, raise_missing: bool = False):
12761273
Targeted labels.
12771274
axis: int
12781275
Dimension on which the indexing is being made.
1279-
raise_missing: bool, default False
1280-
Whether to raise a KeyError if some labels were not found.
1281-
Will be removed in the future, and then this method will always behave as
1282-
if ``raise_missing=True``.
12831276
12841277
Raises
12851278
------
12861279
KeyError
1287-
If at least one key was requested but none was found, and
1288-
raise_missing=True.
1280+
If at least one key was requested but none was found.
12891281
12901282
Returns
12911283
-------
@@ -1310,12 +1302,10 @@ def _get_listlike_indexer(self, key, axis: int, raise_missing: bool = False):
13101302
else:
13111303
keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
13121304

1313-
self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
1305+
self._validate_read_indexer(keyarr, indexer, axis)
13141306
return keyarr, indexer
13151307

1316-
def _validate_read_indexer(
1317-
self, key, indexer, axis: int, raise_missing: bool = False
1318-
):
1308+
def _validate_read_indexer(self, key, indexer, axis: int):
13191309
"""
13201310
Check that indexer can be used to return a result.
13211311
@@ -1331,16 +1321,11 @@ def _validate_read_indexer(
13311321
(with -1 indicating not found).
13321322
axis : int
13331323
Dimension on which the indexing is being made.
1334-
raise_missing: bool
1335-
Whether to raise a KeyError if some labels are not found. Will be
1336-
removed in the future, and then this method will always behave as
1337-
if raise_missing=True.
13381324
13391325
Raises
13401326
------
13411327
KeyError
1342-
If at least one key was requested but none was found, and
1343-
raise_missing=True.
1328+
If at least one key was requested but none was found.
13441329
"""
13451330
if len(key) == 0:
13461331
return
@@ -1356,21 +1341,8 @@ def _validate_read_indexer(
13561341

13571342
ax = self.obj._get_axis(axis)
13581343

1359-
# We (temporarily) allow for some missing keys with .loc, except in
1360-
# some cases (e.g. setting) in which "raise_missing" will be False
1361-
if raise_missing:
1362-
not_found = list(set(key) - set(ax))
1363-
raise KeyError(f"{not_found} not in index")
1364-
1365-
not_found = key[missing_mask]
1366-
1367-
with option_context("display.max_seq_items", 10, "display.width", 80):
1368-
raise KeyError(
1369-
"Passing list-likes to .loc or [] with any missing labels "
1370-
"is no longer supported. "
1371-
f"The following labels were missing: {not_found}. "
1372-
"See https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
1373-
)
1344+
not_found = list(set(key) - set(ax))
1345+
raise KeyError(f"{not_found} not in index")
13741346

13751347

13761348
@doc(IndexingMixin.iloc)

pandas/tests/indexing/test_categorical.py

+7-24
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,7 @@ def test_loc_getitem_listlike_labels(self):
296296
def test_loc_getitem_listlike_unused_category(self):
297297
# GH#37901 a label that is in index.categories but not in index
298298
# listlike containing an element in the categories but not in the values
299-
msg = (
300-
"The following labels were missing: CategoricalIndex(['e'], "
301-
"categories=['c', 'a', 'b', 'e'], ordered=False, name='B', "
302-
"dtype='category')"
303-
)
304-
with pytest.raises(KeyError, match=re.escape(msg)):
299+
with pytest.raises(KeyError, match=re.escape("['e'] not in index")):
305300
self.df2.loc[["a", "b", "e"]]
306301

307302
def test_loc_getitem_label_unused_category(self):
@@ -311,10 +306,7 @@ def test_loc_getitem_label_unused_category(self):
311306

312307
def test_loc_getitem_non_category(self):
313308
# not all labels in the categories
314-
msg = (
315-
"The following labels were missing: Index(['d'], dtype='object', name='B')"
316-
)
317-
with pytest.raises(KeyError, match=re.escape(msg)):
309+
with pytest.raises(KeyError, match=re.escape("['d'] not in index")):
318310
self.df2.loc[["a", "d"]]
319311

320312
def test_loc_setitem_expansion_label_unused_category(self):
@@ -346,8 +338,7 @@ def test_loc_listlike_dtypes(self):
346338
exp = DataFrame({"A": [1, 1, 2], "B": [4, 4, 5]}, index=exp_index)
347339
tm.assert_frame_equal(res, exp, check_index_type=True)
348340

349-
msg = "The following labels were missing: Index(['x'], dtype='object')"
350-
with pytest.raises(KeyError, match=re.escape(msg)):
341+
with pytest.raises(KeyError, match=re.escape("['x'] not in index")):
351342
df.loc[["a", "x"]]
352343

353344
def test_loc_listlike_dtypes_duplicated_categories_and_codes(self):
@@ -370,8 +361,7 @@ def test_loc_listlike_dtypes_duplicated_categories_and_codes(self):
370361
)
371362
tm.assert_frame_equal(res, exp, check_index_type=True)
372363

373-
msg = "The following labels were missing: Index(['x'], dtype='object')"
374-
with pytest.raises(KeyError, match=re.escape(msg)):
364+
with pytest.raises(KeyError, match=re.escape("['x'] not in index")):
375365
df.loc[["a", "x"]]
376366

377367
def test_loc_listlike_dtypes_unused_category(self):
@@ -394,11 +384,10 @@ def test_loc_listlike_dtypes_unused_category(self):
394384
)
395385
tm.assert_frame_equal(res, exp, check_index_type=True)
396386

397-
msg = "The following labels were missing: Index(['x'], dtype='object')"
398-
with pytest.raises(KeyError, match=re.escape(msg)):
387+
with pytest.raises(KeyError, match=re.escape("['x'] not in index")):
399388
df.loc[["a", "x"]]
400389

401-
def test_loc_getitem_listlike_unused_category_raises_keyerro(self):
390+
def test_loc_getitem_listlike_unused_category_raises_keyerror(self):
402391
# key that is an *unused* category raises
403392
index = CategoricalIndex(["a", "b", "a", "c"], categories=list("abcde"))
404393
df = DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]}, index=index)
@@ -407,13 +396,7 @@ def test_loc_getitem_listlike_unused_category_raises_keyerro(self):
407396
# For comparison, check the scalar behavior
408397
df.loc["e"]
409398

410-
msg = (
411-
"Passing list-likes to .loc or [] with any missing labels is no "
412-
"longer supported. The following labels were missing: "
413-
"CategoricalIndex(['e'], categories=['a', 'b', 'c', 'd', 'e'], "
414-
"ordered=False, dtype='category'). See https"
415-
)
416-
with pytest.raises(KeyError, match=re.escape(msg)):
399+
with pytest.raises(KeyError, match=re.escape("['e'] not in index")):
417400
df.loc[["a", "e"]]
418401

419402
def test_ix_categorical_index(self):

pandas/tests/indexing/test_floats.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,10 @@ def test_floating_misc(self, indexer_sl):
535535
result2 = s.iloc[[0, 2, 4]]
536536
tm.assert_series_equal(result1, result2)
537537

538-
with pytest.raises(KeyError, match="with any missing labels"):
538+
with pytest.raises(KeyError, match="not in index"):
539539
indexer_sl(s)[[1.6, 5, 10]]
540540

541-
with pytest.raises(KeyError, match="with any missing labels"):
541+
with pytest.raises(KeyError, match="not in index"):
542542
indexer_sl(s)[[0, 1, 2]]
543543

544544
result = indexer_sl(s)[[2.5, 5]]

pandas/tests/indexing/test_iloc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ def test_iloc_non_unique_indexing(self):
800800
df2 = DataFrame({"A": [0.1] * 1000, "B": [1] * 1000})
801801
df2 = concat([df2, 2 * df2, 3 * df2])
802802

803-
with pytest.raises(KeyError, match="with any missing labels"):
803+
with pytest.raises(KeyError, match="not in index"):
804804
df2.loc[idx]
805805

806806
def test_iloc_empty_list_indexer_is_ok(self):

pandas/tests/indexing/test_indexing.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ def test_dups_fancy_indexing_not_in_order(self):
247247
tm.assert_frame_equal(result, expected)
248248

249249
rows = ["C", "B", "E"]
250-
with pytest.raises(KeyError, match="with any missing labels"):
250+
with pytest.raises(KeyError, match="not in index"):
251251
df.loc[rows]
252252

253253
# see GH5553, make sure we use the right indexer
254254
rows = ["F", "G", "H", "C", "B", "E"]
255-
with pytest.raises(KeyError, match="with any missing labels"):
255+
with pytest.raises(KeyError, match="not in index"):
256256
df.loc[rows]
257257

258258
def test_dups_fancy_indexing_only_missing_label(self):
@@ -274,22 +274,22 @@ def test_dups_fancy_indexing_missing_label(self, vals):
274274

275275
# GH 4619; duplicate indexer with missing label
276276
df = DataFrame({"A": vals})
277-
with pytest.raises(KeyError, match="with any missing labels"):
277+
with pytest.raises(KeyError, match="not in index"):
278278
df.loc[[0, 8, 0]]
279279

280280
def test_dups_fancy_indexing_non_unique(self):
281281

282282
# non unique with non unique selector
283283
df = DataFrame({"test": [5, 7, 9, 11]}, index=["A", "A", "B", "C"])
284-
with pytest.raises(KeyError, match="with any missing labels"):
284+
with pytest.raises(KeyError, match="not in index"):
285285
df.loc[["A", "A", "E"]]
286286

287287
def test_dups_fancy_indexing2(self):
288288
# GH 5835
289289
# dups on index and missing values
290290
df = DataFrame(np.random.randn(5, 5), columns=["A", "B", "B", "B", "A"])
291291

292-
with pytest.raises(KeyError, match="with any missing labels"):
292+
with pytest.raises(KeyError, match="not in index"):
293293
df.loc[:, ["A", "B", "C"]]
294294

295295
def test_dups_fancy_indexing3(self):

pandas/tests/indexing/test_loc.py

+8-41
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ def test_getitem_label_list_with_missing(self):
293293
s = Series(range(3), index=["a", "b", "c"])
294294

295295
# consistency
296-
with pytest.raises(KeyError, match="with any missing labels"):
296+
with pytest.raises(KeyError, match="not in index"):
297297
s[["a", "d"]]
298298

299299
s = Series(range(3))
300-
with pytest.raises(KeyError, match="with any missing labels"):
300+
with pytest.raises(KeyError, match="not in index"):
301301
s[[0, 3]]
302302

303303
@pytest.mark.parametrize("index", [[True, False], [True, False, True, False]])
@@ -349,7 +349,7 @@ def test_loc_to_fail(self):
349349
s.loc[["4"]]
350350

351351
s.loc[-1] = 3
352-
with pytest.raises(KeyError, match="with any missing labels"):
352+
with pytest.raises(KeyError, match="not in index"):
353353
s.loc[[-1, -2]]
354354

355355
s["a"] = 2
@@ -396,7 +396,7 @@ def test_loc_getitem_list_with_fail(self):
396396
s.loc[[3]]
397397

398398
# a non-match and a match
399-
with pytest.raises(KeyError, match="with any missing labels"):
399+
with pytest.raises(KeyError, match="not in index"):
400400
s.loc[[2, 3]]
401401

402402
def test_loc_index(self):
@@ -2249,12 +2249,7 @@ def test_loc_getitem_list_of_labels_categoricalindex_with_na(self, box):
22492249
ser2 = ser[:-1]
22502250
ci2 = ci[1:]
22512251
# but if there are no NAs present, this should raise KeyError
2252-
msg = (
2253-
r"Passing list-likes to .loc or \[\] with any missing labels is no "
2254-
"longer supported. The following labels were missing: "
2255-
r"(Categorical)?Index\(\[nan\], .*\). "
2256-
"See https"
2257-
)
2252+
msg = "not in index"
22582253
with pytest.raises(KeyError, match=msg):
22592254
ser2.loc[box(ci2)]
22602255

@@ -2264,41 +2259,13 @@ def test_loc_getitem_list_of_labels_categoricalindex_with_na(self, box):
22642259
with pytest.raises(KeyError, match=msg):
22652260
ser2.to_frame().loc[box(ci2)]
22662261

2267-
def test_loc_getitem_many_missing_labels_inside_error_message_limited(self):
2268-
# GH#34272
2269-
n = 10000
2270-
missing_labels = [f"missing_{label}" for label in range(n)]
2271-
ser = Series({"a": 1, "b": 2, "c": 3})
2272-
# regex checks labels between 4 and 9995 are replaced with ellipses
2273-
error_message_regex = "missing_4.*\\.\\.\\..*missing_9995"
2274-
with pytest.raises(KeyError, match=error_message_regex):
2275-
ser.loc[["a", "c"] + missing_labels]
2276-
2277-
def test_loc_getitem_missing_labels_inside_matched_in_error_message(self):
2278-
# GH#34272
2279-
ser = Series({"a": 1, "b": 2, "c": 3})
2280-
error_message_regex = "missing_0.*missing_1.*missing_2"
2281-
with pytest.raises(KeyError, match=error_message_regex):
2282-
ser.loc[["a", "b", "missing_0", "c", "missing_1", "missing_2"]]
2283-
2284-
def test_loc_getitem_long_text_missing_labels_inside_error_message_limited(self):
2285-
# GH#34272
2286-
ser = Series({"a": 1, "b": 2, "c": 3})
2287-
missing_labels = [f"long_missing_label_text_{i}" * 5 for i in range(3)]
2288-
# regex checks for very long labels there are new lines between each
2289-
error_message_regex = (
2290-
"long_missing_label_text_0.*\\\\n.*long_missing_label_text_1"
2291-
)
2292-
with pytest.raises(KeyError, match=error_message_regex):
2293-
ser.loc[["a", "c"] + missing_labels]
2294-
22952262
def test_loc_getitem_series_label_list_missing_values(self):
22962263
# gh-11428
22972264
key = np.array(
22982265
["2001-01-04", "2001-01-02", "2001-01-04", "2001-01-14"], dtype="datetime64"
22992266
)
23002267
ser = Series([2, 5, 8, 11], date_range("2001-01-01", freq="D", periods=4))
2301-
with pytest.raises(KeyError, match="with any missing labels"):
2268+
with pytest.raises(KeyError, match="not in index"):
23022269
ser.loc[key]
23032270

23042271
def test_loc_getitem_series_label_list_missing_integer_values(self):
@@ -2307,7 +2274,7 @@ def test_loc_getitem_series_label_list_missing_integer_values(self):
23072274
index=np.array([9730701000001104, 10049011000001109]),
23082275
data=np.array([999000011000001104, 999000011000001104]),
23092276
)
2310-
with pytest.raises(KeyError, match="with any missing labels"):
2277+
with pytest.raises(KeyError, match="not in index"):
23112278
ser.loc[np.array([9730701000001104, 10047311000001102])]
23122279

23132280
@pytest.mark.parametrize("to_period", [True, False])
@@ -2349,7 +2316,7 @@ def test_loc_getitem_listlike_of_datetimelike_keys(self, to_period):
23492316
if to_period:
23502317
keys = [x.to_period("D") for x in keys]
23512318

2352-
with pytest.raises(KeyError, match="with any missing labels"):
2319+
with pytest.raises(KeyError, match="not in index"):
23532320
ser.loc[keys]
23542321

23552322

0 commit comments

Comments
 (0)