Skip to content

Commit 3238c3d

Browse files
jbrockmendelproost
authored andcommitted
DEPR: loc with listlikes with missing elements (pandas-dev#29802)
1 parent de8f942 commit 3238c3d

File tree

11 files changed

+116
-241
lines changed

11 files changed

+116
-241
lines changed

pandas/core/indexing.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1176,18 +1176,12 @@ def _validate_read_indexer(
11761176
# non-missing values), but a bit later in the
11771177
# code, so we want to avoid warning & then
11781178
# just raising
1179-
1180-
_missing_key_warning = textwrap.dedent(
1181-
"""
1182-
Passing list-likes to .loc or [] with any missing label will raise
1183-
KeyError in the future, you can use .reindex() as an alternative.
1184-
1185-
See the documentation here:
1186-
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike""" # noqa: E501
1187-
)
1188-
11891179
if not (ax.is_categorical() or ax.is_interval()):
1190-
warnings.warn(_missing_key_warning, FutureWarning, stacklevel=6)
1180+
raise KeyError(
1181+
"Passing list-likes to .loc or [] with any missing labels "
1182+
"is no longer supported, see "
1183+
"https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike" # noqa:E501
1184+
)
11911185

11921186
def _convert_to_indexer(self, obj, axis: int, raise_missing: bool = False):
11931187
"""

pandas/io/formats/excel.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,12 @@ def __init__(
393393
if not len(Index(cols) & df.columns):
394394
raise KeyError("passes columns are not ALL present dataframe")
395395

396-
# deprecatedin gh-17295
397-
# 1 missing is ok (for now)
398396
if len(Index(cols) & df.columns) != len(cols):
399-
warnings.warn(
400-
"Not all names specified in 'columns' are found; "
401-
"this will raise a KeyError in the future",
402-
FutureWarning,
403-
)
397+
# Deprecated in GH#17295, enforced in 1.0.0
398+
raise KeyError("Not all names specified in 'columns' are found")
399+
400+
self.df = df
404401

405-
self.df = df.reindex(columns=cols)
406402
self.columns = self.df.columns
407403
self.float_format = float_format
408404
self.index = index

pandas/tests/indexing/test_datetime.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from dateutil import tz
44
import numpy as np
5+
import pytest
56

67
import pandas as pd
78
from pandas import DataFrame, Index, Series, Timestamp, date_range
@@ -242,11 +243,8 @@ def test_series_partial_set_datetime(self):
242243
Timestamp("2011-01-02"),
243244
Timestamp("2011-01-03"),
244245
]
245-
exp = Series(
246-
[np.nan, 0.2, np.nan], index=pd.DatetimeIndex(keys, name="idx"), name="s"
247-
)
248-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
249-
tm.assert_series_equal(ser.loc[keys], exp, check_index_type=True)
246+
with pytest.raises(KeyError, match="with any missing labels"):
247+
ser.loc[keys]
250248

251249
def test_series_partial_set_period(self):
252250
# GH 11497
@@ -273,12 +271,8 @@ def test_series_partial_set_period(self):
273271
pd.Period("2011-01-02", freq="D"),
274272
pd.Period("2011-01-03", freq="D"),
275273
]
276-
exp = Series(
277-
[np.nan, 0.2, np.nan], index=pd.PeriodIndex(keys, name="idx"), name="s"
278-
)
279-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
280-
result = ser.loc[keys]
281-
tm.assert_series_equal(result, exp)
274+
with pytest.raises(KeyError, match="with any missing labels"):
275+
ser.loc[keys]
282276

283277
def test_nanosecond_getitem_setitem_with_tz(self):
284278
# GH 11679

pandas/tests/indexing/test_floats.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -726,25 +726,15 @@ def test_floating_misc(self):
726726
tm.assert_series_equal(result1, result3)
727727
tm.assert_series_equal(result1, result4)
728728

729-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
730-
result1 = s[[1.6, 5, 10]]
731-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
732-
result2 = s.loc[[1.6, 5, 10]]
733-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
734-
result3 = s.loc[[1.6, 5, 10]]
735-
tm.assert_series_equal(result1, result2)
736-
tm.assert_series_equal(result1, result3)
737-
tm.assert_series_equal(result1, Series([np.nan, 2, 4], index=[1.6, 5, 10]))
738-
739-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
740-
result1 = s[[0, 1, 2]]
741-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
742-
result2 = s.loc[[0, 1, 2]]
743-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
744-
result3 = s.loc[[0, 1, 2]]
745-
tm.assert_series_equal(result1, result2)
746-
tm.assert_series_equal(result1, result3)
747-
tm.assert_series_equal(result1, Series([0.0, np.nan, np.nan], index=[0, 1, 2]))
729+
with pytest.raises(KeyError, match="with any missing labels"):
730+
s[[1.6, 5, 10]]
731+
with pytest.raises(KeyError, match="with any missing labels"):
732+
s.loc[[1.6, 5, 10]]
733+
734+
with pytest.raises(KeyError, match="with any missing labels"):
735+
s[[0, 1, 2]]
736+
with pytest.raises(KeyError, match="with any missing labels"):
737+
s.loc[[0, 1, 2]]
748738

749739
result1 = s.loc[[2.5, 5]]
750740
result2 = s.loc[[2.5, 5]]

pandas/tests/indexing/test_iloc.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -728,20 +728,8 @@ def test_iloc_non_unique_indexing(self):
728728
df2 = DataFrame({"A": [0.1] * 1000, "B": [1] * 1000})
729729
df2 = concat([df2, 2 * df2, 3 * df2])
730730

731-
sidx = df2.index.to_series()
732-
expected = df2.iloc[idx[idx <= sidx.max()]]
733-
734-
new_list = []
735-
for r, s in expected.iterrows():
736-
new_list.append(s)
737-
new_list.append(s * 2)
738-
new_list.append(s * 3)
739-
740-
expected = DataFrame(new_list)
741-
expected = concat([expected, DataFrame(index=idx[idx > sidx.max()])], sort=True)
742-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
743-
result = df2.loc[idx]
744-
tm.assert_frame_equal(result, expected, check_index_type=False)
731+
with pytest.raises(KeyError, match="with any missing labels"):
732+
df2.loc[idx]
745733

746734
def test_iloc_empty_list_indexer_is_ok(self):
747735

pandas/tests/indexing/test_indexing.py

+12-44
Original file line numberDiff line numberDiff line change
@@ -299,32 +299,13 @@ def test_dups_fancy_indexing(self):
299299
tm.assert_frame_equal(result, expected)
300300

301301
rows = ["C", "B", "E"]
302-
expected = DataFrame(
303-
{
304-
"test": [11, 9, np.nan],
305-
"test1": [7.0, 6, np.nan],
306-
"other": ["d", "c", np.nan],
307-
},
308-
index=rows,
309-
)
310-
311-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
312-
result = df.loc[rows]
313-
tm.assert_frame_equal(result, expected)
302+
with pytest.raises(KeyError, match="with any missing labels"):
303+
df.loc[rows]
314304

315305
# see GH5553, make sure we use the right indexer
316306
rows = ["F", "G", "H", "C", "B", "E"]
317-
expected = DataFrame(
318-
{
319-
"test": [np.nan, np.nan, np.nan, 11, 9, np.nan],
320-
"test1": [np.nan, np.nan, np.nan, 7.0, 6, np.nan],
321-
"other": [np.nan, np.nan, np.nan, "d", "c", np.nan],
322-
},
323-
index=rows,
324-
)
325-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
326-
result = df.loc[rows]
327-
tm.assert_frame_equal(result, expected)
307+
with pytest.raises(KeyError, match="with any missing labels"):
308+
df.loc[rows]
328309

329310
# List containing only missing label
330311
dfnu = DataFrame(np.random.randn(5, 3), index=list("AABCD"))
@@ -340,38 +321,25 @@ def test_dups_fancy_indexing(self):
340321

341322
# GH 4619; duplicate indexer with missing label
342323
df = DataFrame({"A": [0, 1, 2]})
343-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
344-
result = df.loc[[0, 8, 0]]
345-
expected = DataFrame({"A": [0, np.nan, 0]}, index=[0, 8, 0])
346-
tm.assert_frame_equal(result, expected, check_index_type=False)
324+
with pytest.raises(KeyError, match="with any missing labels"):
325+
df.loc[[0, 8, 0]]
347326

348327
df = DataFrame({"A": list("abc")})
349-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
350-
result = df.loc[[0, 8, 0]]
351-
expected = DataFrame({"A": ["a", np.nan, "a"]}, index=[0, 8, 0])
352-
tm.assert_frame_equal(result, expected, check_index_type=False)
328+
with pytest.raises(KeyError, match="with any missing labels"):
329+
df.loc[[0, 8, 0]]
353330

354331
# non unique with non unique selector
355332
df = DataFrame({"test": [5, 7, 9, 11]}, index=["A", "A", "B", "C"])
356-
expected = DataFrame(
357-
{"test": [5, 7, 5, 7, np.nan]}, index=["A", "A", "A", "A", "E"]
358-
)
359-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
360-
result = df.loc[["A", "A", "E"]]
361-
tm.assert_frame_equal(result, expected)
333+
with pytest.raises(KeyError, match="with any missing labels"):
334+
df.loc[["A", "A", "E"]]
362335

363336
def test_dups_fancy_indexing2(self):
364337
# GH 5835
365338
# dups on index and missing values
366339
df = DataFrame(np.random.randn(5, 5), columns=["A", "B", "B", "B", "A"])
367340

368-
expected = pd.concat(
369-
[df.loc[:, ["A", "B"]], DataFrame(np.nan, columns=["C"], index=df.index)],
370-
axis=1,
371-
)
372-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
373-
result = df.loc[:, ["A", "B", "C"]]
374-
tm.assert_frame_equal(result, expected)
341+
with pytest.raises(KeyError, match="with any missing labels"):
342+
df.loc[:, ["A", "B", "C"]]
375343

376344
# GH 6504, multi-axis indexing
377345
df = DataFrame(

pandas/tests/indexing/test_loc.py

+35-43
Original file line numberDiff line numberDiff line change
@@ -159,48 +159,46 @@ def test_loc_getitem_label_list_with_missing(self):
159159
self.check_result(
160160
"loc", [0, 1, 2], "indexer", [0, 1, 2], typs=["empty"], fails=KeyError,
161161
)
162-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
163-
self.check_result(
164-
"loc",
165-
[0, 2, 10],
166-
"ix",
167-
[0, 2, 10],
168-
typs=["ints", "uints", "floats"],
169-
axes=0,
170-
fails=KeyError,
171-
)
162+
self.check_result(
163+
"loc",
164+
[0, 2, 10],
165+
"ix",
166+
[0, 2, 10],
167+
typs=["ints", "uints", "floats"],
168+
axes=0,
169+
fails=KeyError,
170+
)
172171

173-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
174-
self.check_result(
175-
"loc",
176-
[3, 6, 7],
177-
"ix",
178-
[3, 6, 7],
179-
typs=["ints", "uints", "floats"],
180-
axes=1,
181-
fails=KeyError,
182-
)
172+
self.check_result(
173+
"loc",
174+
[3, 6, 7],
175+
"ix",
176+
[3, 6, 7],
177+
typs=["ints", "uints", "floats"],
178+
axes=1,
179+
fails=KeyError,
180+
)
183181

184182
# GH 17758 - MultiIndex and missing keys
185-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
186-
self.check_result(
187-
"loc",
188-
[(1, 3), (1, 4), (2, 5)],
189-
"ix",
190-
[(1, 3), (1, 4), (2, 5)],
191-
typs=["multi"],
192-
axes=0,
193-
)
183+
self.check_result(
184+
"loc",
185+
[(1, 3), (1, 4), (2, 5)],
186+
"ix",
187+
[(1, 3), (1, 4), (2, 5)],
188+
typs=["multi"],
189+
axes=0,
190+
fails=KeyError,
191+
)
194192

195193
def test_getitem_label_list_with_missing(self):
196194
s = Series(range(3), index=["a", "b", "c"])
197195

198196
# consistency
199-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
197+
with pytest.raises(KeyError, match="with any missing labels"):
200198
s[["a", "d"]]
201199

202200
s = Series(range(3))
203-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
201+
with pytest.raises(KeyError, match="with any missing labels"):
204202
s[[0, 3]]
205203

206204
def test_loc_getitem_label_list_fails(self):
@@ -305,10 +303,8 @@ def test_loc_to_fail(self):
305303
s.loc[["4"]]
306304

307305
s.loc[-1] = 3
308-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
309-
result = s.loc[[-1, -2]]
310-
expected = Series([3, np.nan], index=[-1, -2])
311-
tm.assert_series_equal(result, expected)
306+
with pytest.raises(KeyError, match="with any missing labels"):
307+
s.loc[[-1, -2]]
312308

313309
s["a"] = 2
314310
msg = (
@@ -354,10 +350,8 @@ def test_loc_getitem_list_with_fail(self):
354350
s.loc[[3]]
355351

356352
# a non-match and a match
357-
with tm.assert_produces_warning(FutureWarning):
358-
expected = s.loc[[2, 3]]
359-
result = s.reindex([2, 3])
360-
tm.assert_series_equal(result, expected)
353+
with pytest.raises(KeyError, match="with any missing labels"):
354+
s.loc[[2, 3]]
361355

362356
def test_loc_getitem_label_slice(self):
363357

@@ -1034,10 +1028,8 @@ def test_series_loc_getitem_label_list_missing_values():
10341028
["2001-01-04", "2001-01-02", "2001-01-04", "2001-01-14"], dtype="datetime64"
10351029
)
10361030
s = Series([2, 5, 8, 11], date_range("2001-01-01", freq="D", periods=4))
1037-
expected = Series([11.0, 5.0, 11.0, np.nan], index=key)
1038-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1039-
result = s.loc[key]
1040-
tm.assert_series_equal(result, expected)
1031+
with pytest.raises(KeyError, match="with any missing labels"):
1032+
s.loc[key]
10411033

10421034

10431035
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)