Skip to content

Commit 4536853

Browse files
simonjayhawkinsjreback
authored andcommitted
TST/CLN: Add message checks to raises KeyError tests (#27354)
1 parent baba98c commit 4536853

24 files changed

+198
-95
lines changed

pandas/tests/frame/test_axis_select_reindex.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
import re
23

34
import numpy as np
45
import pytest
@@ -1120,9 +1121,10 @@ def test_raise_on_drop_duplicate_index(self, actual):
11201121

11211122
# issue 19186
11221123
level = 0 if isinstance(actual.index, MultiIndex) else None
1123-
with pytest.raises(KeyError):
1124+
msg = re.escape("\"['c'] not found in axis\"")
1125+
with pytest.raises(KeyError, match=msg):
11241126
actual.drop("c", level=level, axis=0)
1125-
with pytest.raises(KeyError):
1127+
with pytest.raises(KeyError, match=msg):
11261128
actual.T.drop("c", level=level, axis=1)
11271129
expected_no_err = actual.drop("c", axis=0, level=level, errors="ignore")
11281130
assert_frame_equal(expected_no_err, actual)

pandas/tests/frame/test_duplicates.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -9,11 +11,12 @@
911
def test_duplicated_with_misspelled_column_name(subset):
1012
# GH 19730
1113
df = DataFrame({"A": [0, 0, 1], "B": [0, 0, 1], "C": [0, 0, 1]})
14+
msg = re.escape("Index(['a'], dtype='object')")
1215

13-
with pytest.raises(KeyError):
16+
with pytest.raises(KeyError, match=msg):
1417
df.duplicated(subset)
1518

16-
with pytest.raises(KeyError):
19+
with pytest.raises(KeyError, match=msg):
1720
df.drop_duplicates(subset)
1821

1922

pandas/tests/frame/test_indexing.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import date, datetime, time, timedelta
2+
import re
23
from warnings import catch_warnings, simplefilter
34

45
import numpy as np
@@ -59,15 +60,16 @@ def test_getitem(self, float_frame):
5960
ad = np.random.randn(len(df))
6061
df["@awesome_domain"] = ad
6162

62-
with pytest.raises(KeyError):
63+
with pytest.raises(KeyError, match=re.escape("'df[\"$10\"]'")):
6364
df.__getitem__('df["$10"]')
6465

6566
res = df["@awesome_domain"]
6667
tm.assert_numpy_array_equal(ad, res.values)
6768

6869
def test_getitem_dupe_cols(self):
6970
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "a", "b"])
70-
with pytest.raises(KeyError):
71+
msg = "\"None of [Index(['baf'], dtype='object')] are in the [columns]\""
72+
with pytest.raises(KeyError, match=re.escape(msg)):
7173
df[["baf"]]
7274

7375
def test_get(self, float_frame):
@@ -446,14 +448,16 @@ def test_getitem_setitem_ix_negative_integers(self, float_frame):
446448

447449
df = DataFrame(np.random.randn(8, 4))
448450
# ix does label-based indexing when having an integer index
451+
msg = "\"None of [Int64Index([-1], dtype='int64')] are in the [index]\""
449452
with catch_warnings(record=True):
450453
simplefilter("ignore", FutureWarning)
451-
with pytest.raises(KeyError):
454+
with pytest.raises(KeyError, match=re.escape(msg)):
452455
df.ix[[-1]]
453456

457+
msg = "\"None of [Int64Index([-1], dtype='int64')] are in the [columns]\""
454458
with catch_warnings(record=True):
455459
simplefilter("ignore", FutureWarning)
456-
with pytest.raises(KeyError):
460+
with pytest.raises(KeyError, match=re.escape(msg)):
457461
df.ix[:, [-1]]
458462

459463
# #1942
@@ -497,7 +501,11 @@ def test_setitem(self, float_frame):
497501
float_frame["col6"] = series
498502
tm.assert_series_equal(series, float_frame["col6"], check_names=False)
499503

500-
with pytest.raises(KeyError):
504+
msg = (
505+
r"\"None of \[Float64Index\(\[.*dtype='float64'\)\] are in the"
506+
r" \[columns\]\""
507+
)
508+
with pytest.raises(KeyError, match=msg):
501509
float_frame[np.random.randn(len(float_frame) + 1)] = 1
502510

503511
# set ndarray
@@ -1885,10 +1893,10 @@ def test_lookup_bool(self):
18851893
assert df["mask"].dtype == np.bool_
18861894

18871895
def test_lookup_raises(self, float_frame):
1888-
with pytest.raises(KeyError):
1896+
with pytest.raises(KeyError, match="'One or more row labels was not found'"):
18891897
float_frame.lookup(["xyz"], ["A"])
18901898

1891-
with pytest.raises(KeyError):
1899+
with pytest.raises(KeyError, match="'One or more column labels was not found'"):
18921900
float_frame.lookup([float_frame.index[0]], ["xyz"])
18931901

18941902
with pytest.raises(ValueError, match="same size"):
@@ -2544,7 +2552,9 @@ def test_xs(self, float_frame, datetime_frame):
25442552
assert xs["A"] == 1
25452553
assert xs["B"] == "1"
25462554

2547-
with pytest.raises(KeyError):
2555+
with pytest.raises(
2556+
KeyError, match=re.escape("Timestamp('1999-12-31 00:00:00', freq='B')")
2557+
):
25482558
datetime_frame.xs(datetime_frame.index[0] - BDay())
25492559

25502560
# xs get column

pandas/tests/frame/test_mutate_columns.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -88,9 +90,9 @@ def test_assign_dependent_old_python(self):
8890
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
8991

9092
# Key C does not exist at definition time of df
91-
with pytest.raises(KeyError):
93+
with pytest.raises(KeyError, match="^'C'$"):
9294
df.assign(C=lambda df: df.A, D=lambda df: df["A"] + df["C"])
93-
with pytest.raises(KeyError):
95+
with pytest.raises(KeyError, match="^'C'$"):
9496
df.assign(C=df.A, D=lambda x: x["A"] + x["C"])
9597

9698
@pytest.mark.skipif(
@@ -219,14 +221,14 @@ def test_delitem_multiindex(self):
219221
# A still in the levels, BUT get a KeyError if trying
220222
# to delete
221223
assert ("A",) not in df.columns
222-
with pytest.raises(KeyError):
224+
with pytest.raises(KeyError, match=re.escape("('A',)")):
223225
del df[("A",)]
224226

225227
# behavior of dropped/deleted MultiIndex levels changed from
226228
# GH 2770 to GH 19027: MultiIndex no longer '.__contains__'
227229
# levels which are dropped/deleted
228230
assert "A" not in df.columns
229-
with pytest.raises(KeyError):
231+
with pytest.raises(KeyError, match=re.escape("('A',)")):
230232
del df["A"]
231233

232234
def test_pop(self, float_frame):

pandas/tests/generic/test_generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ def test_sample(sel):
618618
df.sample(n=1, weights="weight_column", axis=1)
619619

620620
# Check weighting key error
621-
with pytest.raises(KeyError):
621+
with pytest.raises(
622+
KeyError, match="'String passed to weights not a valid column'"
623+
):
622624
df.sample(n=3, weights="not_a_real_column_name")
623625

624626
# Check that re-normalizes weights that don't sum to one.

pandas/tests/groupby/test_timegrouper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_timegrouper_with_reg_groups(self):
206206
result = df.groupby([pd.Grouper(freq="1M", key="Date"), "Buyer"]).sum()
207207
assert_frame_equal(result, expected)
208208

209-
with pytest.raises(KeyError):
209+
with pytest.raises(KeyError, match="'The grouper name foo is not found'"):
210210
df.groupby([pd.Grouper(freq="1M", key="foo"), "Buyer"]).sum()
211211

212212
# passing the level

pandas/tests/indexes/datetimes/test_indexing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def test_get_loc(self):
614614
)
615615
with pytest.raises(ValueError, match="unit abbreviation w/o a number"):
616616
idx.get_loc("2000-01-01T12", method="nearest", tolerance="foo")
617-
with pytest.raises(KeyError):
617+
with pytest.raises(KeyError, match="'2000-01-01T03'"):
618618
idx.get_loc("2000-01-01T03", method="nearest", tolerance="2 hours")
619619
with pytest.raises(
620620
ValueError, match="tolerance size must match target index size"
@@ -634,12 +634,12 @@ def test_get_loc(self):
634634
assert idx.get_loc("1999", method="nearest") == 0
635635
assert idx.get_loc("2001", method="nearest") == 2
636636

637-
with pytest.raises(KeyError):
637+
with pytest.raises(KeyError, match="'1999'"):
638638
idx.get_loc("1999", method="pad")
639-
with pytest.raises(KeyError):
639+
with pytest.raises(KeyError, match="'2001'"):
640640
idx.get_loc("2001", method="backfill")
641641

642-
with pytest.raises(KeyError):
642+
with pytest.raises(KeyError, match="'foobar'"):
643643
idx.get_loc("foobar")
644644
with pytest.raises(TypeError):
645645
idx.get_loc(slice(2))

pandas/tests/indexes/interval/test_interval.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def test_get_loc_length_one_scalar(self, scalar, closed):
445445
result = index.get_loc(scalar)
446446
assert result == 0
447447
else:
448-
with pytest.raises(KeyError):
448+
with pytest.raises(KeyError, match=str(scalar)):
449449
index.get_loc(scalar)
450450

451451
@pytest.mark.parametrize("other_closed", ["left", "right", "both", "neither"])
@@ -458,7 +458,14 @@ def test_get_loc_length_one_interval(self, left, right, closed, other_closed):
458458
result = index.get_loc(interval)
459459
assert result == 0
460460
else:
461-
with pytest.raises(KeyError):
461+
with pytest.raises(
462+
KeyError,
463+
match=re.escape(
464+
"Interval({left}, {right}, closed='{other_closed}')".format(
465+
left=left, right=right, other_closed=other_closed
466+
)
467+
),
468+
):
462469
index.get_loc(interval)
463470

464471
# Make consistent with test_interval_new.py (see #16316, #16386)

pandas/tests/indexes/interval/test_interval_new.py

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -15,16 +17,21 @@ def test_get_loc_interval(self, closed, side):
1517
for bound in [[0, 1], [1, 2], [2, 3], [3, 4], [0, 2], [2.5, 3], [-1, 4]]:
1618
# if get_loc is supplied an interval, it should only search
1719
# for exact matches, not overlaps or covers, else KeyError.
20+
msg = re.escape(
21+
"Interval({bound[0]}, {bound[1]}, closed='{side}')".format(
22+
bound=bound, side=side
23+
)
24+
)
1825
if closed == side:
1926
if bound == [0, 1]:
2027
assert idx.get_loc(Interval(0, 1, closed=side)) == 0
2128
elif bound == [2, 3]:
2229
assert idx.get_loc(Interval(2, 3, closed=side)) == 1
2330
else:
24-
with pytest.raises(KeyError):
31+
with pytest.raises(KeyError, match=msg):
2532
idx.get_loc(Interval(*bound, closed=side))
2633
else:
27-
with pytest.raises(KeyError):
34+
with pytest.raises(KeyError, match=msg):
2835
idx.get_loc(Interval(*bound, closed=side))
2936

3037
@pytest.mark.parametrize("scalar", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5])
@@ -81,18 +88,42 @@ def test_slice_locs_with_interval(self):
8188
# unsorted duplicates
8289
index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)])
8390

84-
with pytest.raises(KeyError):
91+
with pytest.raises(
92+
KeyError,
93+
match=re.escape(
94+
'"Cannot get left slice bound for non-unique label:'
95+
" Interval(0, 2, closed='right')\""
96+
),
97+
):
8598
index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))
8699

87-
with pytest.raises(KeyError):
100+
with pytest.raises(
101+
KeyError,
102+
match=re.escape(
103+
'"Cannot get left slice bound for non-unique label:'
104+
" Interval(0, 2, closed='right')\""
105+
),
106+
):
88107
index.slice_locs(start=Interval(0, 2))
89108

90109
assert index.slice_locs(end=Interval(2, 4)) == (0, 2)
91110

92-
with pytest.raises(KeyError):
111+
with pytest.raises(
112+
KeyError,
113+
match=re.escape(
114+
'"Cannot get right slice bound for non-unique label:'
115+
" Interval(0, 2, closed='right')\""
116+
),
117+
):
93118
index.slice_locs(end=Interval(0, 2))
94119

95-
with pytest.raises(KeyError):
120+
with pytest.raises(
121+
KeyError,
122+
match=re.escape(
123+
'"Cannot get right slice bound for non-unique label:'
124+
" Interval(0, 2, closed='right')\""
125+
),
126+
):
96127
index.slice_locs(start=Interval(2, 4), end=Interval(0, 2))
97128

98129
# another unsorted duplicates
@@ -139,7 +170,13 @@ def test_slice_locs_with_ints_and_floats_succeeds(self):
139170
def test_slice_locs_with_ints_and_floats_errors(self, tuples, query):
140171
start, stop = query
141172
index = IntervalIndex.from_tuples(tuples)
142-
with pytest.raises(KeyError):
173+
with pytest.raises(
174+
KeyError,
175+
match=(
176+
"'can only get slices from an IntervalIndex if bounds are"
177+
" non-overlapping and all monotonic increasing or decreasing'"
178+
),
179+
):
143180
index.slice_locs(start, stop)
144181

145182
@pytest.mark.parametrize(

pandas/tests/indexes/interval/test_interval_tree.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ def test_get_loc(self, tree):
6262
expected = np.array([0, 1], dtype="intp")
6363
tm.assert_numpy_array_equal(result, expected)
6464

65-
with pytest.raises(KeyError):
65+
with pytest.raises(KeyError, match="-1"):
6666
tree.get_loc(-1)
6767

6868
def test_get_indexer(self, tree):
6969
result = tree.get_indexer(np.array([1.0, 5.5, 6.5]))
7070
expected = np.array([0, 4, -1], dtype="intp")
7171
tm.assert_numpy_array_equal(result, expected)
7272

73-
with pytest.raises(KeyError):
73+
with pytest.raises(
74+
KeyError, match="'indexer does not intersect a unique set of intervals'"
75+
):
7476
tree.get_indexer(np.array([3.0]))
7577

7678
def test_get_indexer_non_unique(self, tree):
@@ -100,7 +102,9 @@ def test_duplicates(self, dtype):
100102
expected = np.array([0, 1, 2], dtype="intp")
101103
tm.assert_numpy_array_equal(result, expected)
102104

103-
with pytest.raises(KeyError):
105+
with pytest.raises(
106+
KeyError, match="'indexer does not intersect a unique set of intervals'"
107+
):
104108
tree.get_indexer(np.array([0.5]))
105109

106110
indexer, missing = tree.get_indexer_non_unique(np.array([0.5]))
@@ -116,7 +120,7 @@ def test_get_loc_closed(self, closed):
116120
tree = IntervalTree([0], [1], closed=closed)
117121
for p, errors in [(0, tree.open_left), (1, tree.open_right)]:
118122
if errors:
119-
with pytest.raises(KeyError):
123+
with pytest.raises(KeyError, match=str(p)):
120124
tree.get_loc(p)
121125
else:
122126
result = tree.get_loc(p)

pandas/tests/indexes/multi/test_partial_indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_partial_string_timestamp_multiindex():
5454
# ambiguous and we don't want to extend this behavior forward to work
5555
# in multi-indexes. This would amount to selecting a scalar from a
5656
# column.
57-
with pytest.raises(KeyError):
57+
with pytest.raises(KeyError, match="'2016-01-01'"):
5858
df["2016-01-01"]
5959

6060
# partial string match on year only
@@ -83,7 +83,7 @@ def test_partial_string_timestamp_multiindex():
8383
tm.assert_frame_equal(result, expected)
8484

8585
# Slicing date on first level should break (of course)
86-
with pytest.raises(KeyError):
86+
with pytest.raises(KeyError, match="'2016-01-01'"):
8787
df_swap.loc["2016-01-01"]
8888

8989
# GH12685 (partial string with daily resolution or below)

pandas/tests/indexes/multi/test_sorting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_unsortedindex():
115115
df.sort_index(inplace=True)
116116
assert len(df.loc(axis=0)["z", :]) == 2
117117

118-
with pytest.raises(KeyError):
118+
with pytest.raises(KeyError, match="'q'"):
119119
df.loc(axis=0)["q", :]
120120

121121

pandas/tests/indexes/period/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_getitem_day(self):
200200

201201
invalid = ["2013/02/01 9H", "2013/02/01 09:00"]
202202
for v in invalid:
203-
with pytest.raises(KeyError):
203+
with pytest.raises(KeyError, match=v):
204204
s[v]
205205

206206

0 commit comments

Comments
 (0)