Skip to content

Commit f78790f

Browse files
simonjayhawkinsjreback
authored andcommitted
STY: use pytest.raises context syntax (reshape) (#24838)
1 parent 1dcfc5f commit f78790f

File tree

6 files changed

+204
-114
lines changed

6 files changed

+204
-114
lines changed

pandas/core/reshape/merge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ def _validate(self, validate):
10871087
elif validate in ["one_to_many", "1:m"]:
10881088
if not left_unique:
10891089
raise MergeError("Merge keys are not unique in left dataset;"
1090-
"not a one-to-many merge")
1090+
" not a one-to-many merge")
10911091

10921092
elif validate in ["many_to_one", "m:1"]:
10931093
if not right_unique:

pandas/tests/reshape/merge/test_join.py

+47-29
Original file line numberDiff line numberDiff line change
@@ -195,38 +195,47 @@ def test_join_on(self):
195195
assert np.isnan(joined['three']['c'])
196196

197197
# merge column not p resent
198-
pytest.raises(KeyError, target.join, source, on='E')
198+
with pytest.raises(KeyError, match="^'E'$"):
199+
target.join(source, on='E')
199200

200201
# overlap
201202
source_copy = source.copy()
202203
source_copy['A'] = 0
203-
pytest.raises(ValueError, target.join, source_copy, on='A')
204+
msg = ("You are trying to merge on float64 and object columns. If"
205+
" you wish to proceed you should use pd.concat")
206+
with pytest.raises(ValueError, match=msg):
207+
target.join(source_copy, on='A')
204208

205209
def test_join_on_fails_with_different_right_index(self):
206-
with pytest.raises(ValueError):
207-
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
208-
'b': np.random.randn(3)})
209-
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
210-
'b': np.random.randn(10)},
211-
index=tm.makeCustomIndex(10, 2))
210+
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
211+
'b': np.random.randn(3)})
212+
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
213+
'b': np.random.randn(10)},
214+
index=tm.makeCustomIndex(10, 2))
215+
msg = (r'len\(left_on\) must equal the number of levels in the index'
216+
' of "right"')
217+
with pytest.raises(ValueError, match=msg):
212218
merge(df, df2, left_on='a', right_index=True)
213219

214220
def test_join_on_fails_with_different_left_index(self):
215-
with pytest.raises(ValueError):
216-
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
217-
'b': np.random.randn(3)},
218-
index=tm.makeCustomIndex(10, 2))
219-
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
220-
'b': np.random.randn(10)})
221+
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
222+
'b': np.random.randn(3)},
223+
index=tm.makeCustomIndex(3, 2))
224+
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
225+
'b': np.random.randn(10)})
226+
msg = (r'len\(right_on\) must equal the number of levels in the index'
227+
' of "left"')
228+
with pytest.raises(ValueError, match=msg):
221229
merge(df, df2, right_on='b', left_index=True)
222230

223231
def test_join_on_fails_with_different_column_counts(self):
224-
with pytest.raises(ValueError):
225-
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
226-
'b': np.random.randn(3)})
227-
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
228-
'b': np.random.randn(10)},
229-
index=tm.makeCustomIndex(10, 2))
232+
df = DataFrame({'a': np.random.choice(['m', 'f'], size=3),
233+
'b': np.random.randn(3)})
234+
df2 = DataFrame({'a': np.random.choice(['m', 'f'], size=10),
235+
'b': np.random.randn(10)},
236+
index=tm.makeCustomIndex(10, 2))
237+
msg = r"len\(right_on\) must equal len\(left_on\)"
238+
with pytest.raises(ValueError, match=msg):
230239
merge(df, df2, right_on='a', left_on=['a', 'b'])
231240

232241
@pytest.mark.parametrize("wrong_type", [2, 'str', None, np.array([0, 1])])
@@ -237,9 +246,11 @@ def test_join_on_fails_with_wrong_object_type(self, wrong_type):
237246
# Edited test to remove the Series object from test parameters
238247

239248
df = DataFrame({'a': [1, 1]})
240-
with pytest.raises(TypeError, match=str(type(wrong_type))):
249+
msg = ("Can only merge Series or DataFrame objects, a {} was passed"
250+
.format(str(type(wrong_type))))
251+
with pytest.raises(TypeError, match=msg):
241252
merge(wrong_type, df, left_on='a', right_on='a')
242-
with pytest.raises(TypeError, match=str(type(wrong_type))):
253+
with pytest.raises(TypeError, match=msg):
243254
merge(df, wrong_type, left_on='a', right_on='a')
244255

245256
def test_join_on_pass_vector(self):
@@ -603,7 +614,9 @@ def _check_diff_index(df_list, result, exp_index):
603614
joined = df_list[0].join(df_list[1:], how='inner')
604615
_check_diff_index(df_list, joined, df.index[2:8])
605616

606-
pytest.raises(ValueError, df_list[0].join, df_list[1:], on='a')
617+
msg = "Joining multiple DataFrames only supported for joining on index"
618+
with pytest.raises(ValueError, match=msg):
619+
df_list[0].join(df_list[1:], on='a')
607620

608621
def test_join_many_mixed(self):
609622
df = DataFrame(np.random.randn(8, 4), columns=['A', 'B', 'C', 'D'])
@@ -725,10 +738,13 @@ def test_panel_join_many(self):
725738
tm.assert_panel_equal(joined, expected)
726739

727740
# edge cases
728-
pytest.raises(ValueError, panels[0].join, panels[1:],
729-
how='outer', lsuffix='foo', rsuffix='bar')
730-
pytest.raises(ValueError, panels[0].join, panels[1:],
731-
how='right')
741+
msg = "Suffixes not supported when passing multiple panels"
742+
with pytest.raises(ValueError, match=msg):
743+
panels[0].join(panels[1:], how='outer', lsuffix='foo',
744+
rsuffix='bar')
745+
msg = "Right join not supported with multiple panels"
746+
with pytest.raises(ValueError, match=msg):
747+
panels[0].join(panels[1:], how='right')
732748

733749
def test_join_multi_to_multi(self, join_type):
734750
# GH 20475
@@ -749,10 +765,12 @@ def test_join_multi_to_multi(self, join_type):
749765
)
750766
assert_frame_equal(expected, result)
751767

752-
with pytest.raises(ValueError):
768+
msg = (r'len\(left_on\) must equal the number of levels in the index'
769+
' of "right"')
770+
with pytest.raises(ValueError, match=msg):
753771
left.join(right, on='xy', how=join_type)
754772

755-
with pytest.raises(ValueError):
773+
with pytest.raises(ValueError, match=msg):
756774
right.join(left, on=['abc', 'xy'], how=join_type)
757775

758776

pandas/tests/reshape/merge/test_merge.py

+66-34
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,37 @@ def test_merge_index_singlekey_inner(self):
119119
assert_frame_equal(result, expected.loc[:, result.columns])
120120

121121
def test_merge_misspecified(self):
122-
pytest.raises(ValueError, merge, self.left, self.right,
123-
left_index=True)
124-
pytest.raises(ValueError, merge, self.left, self.right,
125-
right_index=True)
126-
127-
pytest.raises(ValueError, merge, self.left, self.left,
128-
left_on='key', on='key')
129-
130-
pytest.raises(ValueError, merge, self.df, self.df2,
131-
left_on=['key1'], right_on=['key1', 'key2'])
122+
msg = "Must pass right_on or right_index=True"
123+
with pytest.raises(pd.errors.MergeError, match=msg):
124+
merge(self.left, self.right, left_index=True)
125+
msg = "Must pass left_on or left_index=True"
126+
with pytest.raises(pd.errors.MergeError, match=msg):
127+
merge(self.left, self.right, right_index=True)
128+
129+
msg = ('Can only pass argument "on" OR "left_on" and "right_on", not'
130+
' a combination of both')
131+
with pytest.raises(pd.errors.MergeError, match=msg):
132+
merge(self.left, self.left, left_on='key', on='key')
133+
134+
msg = r"len\(right_on\) must equal len\(left_on\)"
135+
with pytest.raises(ValueError, match=msg):
136+
merge(self.df, self.df2, left_on=['key1'],
137+
right_on=['key1', 'key2'])
132138

133139
def test_index_and_on_parameters_confusion(self):
134-
pytest.raises(ValueError, merge, self.df, self.df2, how='left',
135-
left_index=False, right_index=['key1', 'key2'])
136-
pytest.raises(ValueError, merge, self.df, self.df2, how='left',
137-
left_index=['key1', 'key2'], right_index=False)
138-
pytest.raises(ValueError, merge, self.df, self.df2, how='left',
139-
left_index=['key1', 'key2'],
140-
right_index=['key1', 'key2'])
140+
msg = ("right_index parameter must be of type bool, not"
141+
r" <(class|type) 'list'>")
142+
with pytest.raises(ValueError, match=msg):
143+
merge(self.df, self.df2, how='left',
144+
left_index=False, right_index=['key1', 'key2'])
145+
msg = ("left_index parameter must be of type bool, not "
146+
r"<(class|type) 'list'>")
147+
with pytest.raises(ValueError, match=msg):
148+
merge(self.df, self.df2, how='left',
149+
left_index=['key1', 'key2'], right_index=False)
150+
with pytest.raises(ValueError, match=msg):
151+
merge(self.df, self.df2, how='left',
152+
left_index=['key1', 'key2'], right_index=['key1', 'key2'])
141153

142154
def test_merge_overlap(self):
143155
merged = merge(self.left, self.left, on='key')
@@ -269,7 +281,6 @@ def test_no_overlap_more_informative_error(self):
269281
df1 = DataFrame({'x': ['a']}, index=[dt])
270282

271283
df2 = DataFrame({'y': ['b', 'c']}, index=[dt, dt])
272-
pytest.raises(MergeError, merge, df1, df2)
273284

274285
msg = ('No common columns to perform merge on. '
275286
'Merge options: left_on={lon}, right_on={ron}, '
@@ -566,7 +577,10 @@ def test_overlapping_columns_error_message(self):
566577

567578
# #2649, #10639
568579
df2.columns = ['key1', 'foo', 'foo']
569-
pytest.raises(ValueError, merge, df, df2)
580+
msg = (r"Data columns not unique: Index\(\[u?'foo', u?'foo'\],"
581+
r" dtype='object'\)")
582+
with pytest.raises(MergeError, match=msg):
583+
merge(df, df2)
570584

571585
def test_merge_on_datetime64tz(self):
572586

@@ -717,9 +731,10 @@ def test_indicator(self):
717731
assert_frame_equal(test_custom_name, df_result_custom_name)
718732

719733
# Check only accepts strings and booleans
720-
with pytest.raises(ValueError):
734+
msg = "indicator option can only accept boolean or string arguments"
735+
with pytest.raises(ValueError, match=msg):
721736
merge(df1, df2, on='col1', how='outer', indicator=5)
722-
with pytest.raises(ValueError):
737+
with pytest.raises(ValueError, match=msg):
723738
df1.merge(df2, on='col1', how='outer', indicator=5)
724739

725740
# Check result integrity
@@ -743,20 +758,25 @@ def test_indicator(self):
743758
for i in ['_right_indicator', '_left_indicator', '_merge']:
744759
df_badcolumn = DataFrame({'col1': [1, 2], i: [2, 2]})
745760

746-
with pytest.raises(ValueError):
761+
msg = ("Cannot use `indicator=True` option when data contains a"
762+
" column named {}|"
763+
"Cannot use name of an existing column for indicator"
764+
" column").format(i)
765+
with pytest.raises(ValueError, match=msg):
747766
merge(df1, df_badcolumn, on='col1',
748767
how='outer', indicator=True)
749-
with pytest.raises(ValueError):
768+
with pytest.raises(ValueError, match=msg):
750769
df1.merge(df_badcolumn, on='col1', how='outer', indicator=True)
751770

752771
# Check for name conflict with custom name
753772
df_badcolumn = DataFrame(
754773
{'col1': [1, 2], 'custom_column_name': [2, 2]})
755774

756-
with pytest.raises(ValueError):
775+
msg = "Cannot use name of an existing column for indicator column"
776+
with pytest.raises(ValueError, match=msg):
757777
merge(df1, df_badcolumn, on='col1', how='outer',
758778
indicator='custom_column_name')
759-
with pytest.raises(ValueError):
779+
with pytest.raises(ValueError, match=msg):
760780
df1.merge(df_badcolumn, on='col1', how='outer',
761781
indicator='custom_column_name')
762782

@@ -843,11 +863,13 @@ def test_validation(self):
843863
merge(left, right_w_dups, left_index=True, right_index=True,
844864
validate='one_to_many')
845865

846-
with pytest.raises(MergeError):
866+
msg = ("Merge keys are not unique in right dataset; not a one-to-one"
867+
" merge")
868+
with pytest.raises(MergeError, match=msg):
847869
merge(left, right_w_dups, left_index=True, right_index=True,
848870
validate='one_to_one')
849871

850-
with pytest.raises(MergeError):
872+
with pytest.raises(MergeError, match=msg):
851873
merge(left, right_w_dups, on='a', validate='one_to_one')
852874

853875
# Dups on left
@@ -856,26 +878,33 @@ def test_validation(self):
856878
merge(left_w_dups, right, left_index=True, right_index=True,
857879
validate='many_to_one')
858880

859-
with pytest.raises(MergeError):
881+
msg = ("Merge keys are not unique in left dataset; not a one-to-one"
882+
" merge")
883+
with pytest.raises(MergeError, match=msg):
860884
merge(left_w_dups, right, left_index=True, right_index=True,
861885
validate='one_to_one')
862886

863-
with pytest.raises(MergeError):
887+
with pytest.raises(MergeError, match=msg):
864888
merge(left_w_dups, right, on='a', validate='one_to_one')
865889

866890
# Dups on both
867891
merge(left_w_dups, right_w_dups, on='a', validate='many_to_many')
868892

869-
with pytest.raises(MergeError):
893+
msg = ("Merge keys are not unique in right dataset; not a many-to-one"
894+
" merge")
895+
with pytest.raises(MergeError, match=msg):
870896
merge(left_w_dups, right_w_dups, left_index=True,
871897
right_index=True, validate='many_to_one')
872898

873-
with pytest.raises(MergeError):
899+
msg = ("Merge keys are not unique in left dataset; not a one-to-many"
900+
" merge")
901+
with pytest.raises(MergeError, match=msg):
874902
merge(left_w_dups, right_w_dups, on='a',
875903
validate='one_to_many')
876904

877905
# Check invalid arguments
878-
with pytest.raises(ValueError):
906+
msg = "Not a valid argument for validate"
907+
with pytest.raises(ValueError, match=msg):
879908
merge(left, right, on='a', validate='jibberish')
880909

881910
# Two column merge, dups in both, but jointly no dups.
@@ -896,7 +925,9 @@ def test_validation(self):
896925
'um... weasel noise?']},
897926
index=range(3))
898927

899-
with pytest.raises(MergeError):
928+
msg = ("Merge keys are not unique in either left or right dataset;"
929+
" not a one-to-one merge")
930+
with pytest.raises(MergeError, match=msg):
900931
merge(left, right, on='a', validate='1:1')
901932

902933
result = merge(left, right, on=['a', 'b'], validate='1:1')
@@ -1439,6 +1470,7 @@ def test_merge_series(on, left_on, right_on, left_index, right_index, nm):
14391470
left_index=left_index, right_index=right_index)
14401471
tm.assert_frame_equal(result, expected)
14411472
else:
1442-
with pytest.raises(ValueError, match='a Series without a name'):
1473+
msg = "Cannot merge a Series without a name"
1474+
with pytest.raises(ValueError, match=msg):
14431475
result = pd.merge(a, b, on=on, left_on=left_on, right_on=right_on,
14441476
left_index=left_index, right_index=right_index)

0 commit comments

Comments
 (0)