@@ -223,27 +223,29 @@ def test_multi_index(self):
223
223
# MultiIndex is prohibited
224
224
trades = self .trades .set_index (["time" , "price" ])
225
225
quotes = self .quotes .set_index ("time" )
226
- with pytest .raises (MergeError ):
226
+ with pytest .raises (MergeError , match = "left can only have one index" ):
227
227
merge_asof (trades , quotes , left_index = True , right_index = True )
228
228
229
229
trades = self .trades .set_index ("time" )
230
230
quotes = self .quotes .set_index (["time" , "bid" ])
231
- with pytest .raises (MergeError ):
231
+ with pytest .raises (MergeError , match = "right can only have one index" ):
232
232
merge_asof (trades , quotes , left_index = True , right_index = True )
233
233
234
234
def test_on_and_index (self ):
235
235
236
236
# "on" parameter and index together is prohibited
237
237
trades = self .trades .set_index ("time" )
238
238
quotes = self .quotes .set_index ("time" )
239
- with pytest .raises (MergeError ):
239
+ msg = 'Can only pass argument "left_on" OR "left_index" not both.'
240
+ with pytest .raises (MergeError , match = msg ):
240
241
merge_asof (
241
242
trades , quotes , left_on = "price" , left_index = True , right_index = True
242
243
)
243
244
244
245
trades = self .trades .set_index ("time" )
245
246
quotes = self .quotes .set_index ("time" )
246
- with pytest .raises (MergeError ):
247
+ msg = 'Can only pass argument "right_on" OR "right_index" not both.'
248
+ with pytest .raises (MergeError , match = msg ):
247
249
merge_asof (
248
250
trades , quotes , right_on = "bid" , left_index = True , right_index = True
249
251
)
@@ -439,7 +441,9 @@ def test_multiby_indexed(self):
439
441
440
442
tm .assert_frame_equal (expected , result )
441
443
442
- with pytest .raises (MergeError ):
444
+ with pytest .raises (
445
+ MergeError , match = "left_by and right_by must be same length"
446
+ ):
443
447
pd .merge_asof (
444
448
left ,
445
449
right ,
@@ -478,13 +482,15 @@ def test_valid_join_keys(self):
478
482
trades = self .trades
479
483
quotes = self .quotes
480
484
481
- with pytest .raises (MergeError ):
485
+ msg = r"incompatible merge keys \[1\] .* must be the same type"
486
+
487
+ with pytest .raises (MergeError , match = msg ):
482
488
merge_asof (trades , quotes , left_on = "time" , right_on = "bid" , by = "ticker" )
483
489
484
- with pytest .raises (MergeError ):
490
+ with pytest .raises (MergeError , match = "can only asof on a key for left" ):
485
491
merge_asof (trades , quotes , on = ["time" , "ticker" ], by = "ticker" )
486
492
487
- with pytest .raises (MergeError ):
493
+ with pytest .raises (MergeError , match = "can only asof on a key for left" ):
488
494
merge_asof (trades , quotes , by = "ticker" )
489
495
490
496
def test_with_duplicates (self , datapath ):
@@ -513,7 +519,9 @@ def test_valid_allow_exact_matches(self):
513
519
trades = self .trades
514
520
quotes = self .quotes
515
521
516
- with pytest .raises (MergeError ):
522
+ msg = "allow_exact_matches must be boolean, passed foo"
523
+
524
+ with pytest .raises (MergeError , match = msg ):
517
525
merge_asof (
518
526
trades , quotes , on = "time" , by = "ticker" , allow_exact_matches = "foo"
519
527
)
@@ -535,12 +543,14 @@ def test_valid_tolerance(self):
535
543
tolerance = 1 ,
536
544
)
537
545
546
+ msg = r"incompatible tolerance .*, must be compat with type .*"
547
+
538
548
# incompat
539
- with pytest .raises (MergeError ):
549
+ with pytest .raises (MergeError , match = msg ):
540
550
merge_asof (trades , quotes , on = "time" , by = "ticker" , tolerance = 1 )
541
551
542
552
# invalid
543
- with pytest .raises (MergeError ):
553
+ with pytest .raises (MergeError , match = msg ):
544
554
merge_asof (
545
555
trades .reset_index (),
546
556
quotes .reset_index (),
@@ -549,13 +559,15 @@ def test_valid_tolerance(self):
549
559
tolerance = 1.0 ,
550
560
)
551
561
562
+ msg = "tolerance must be positive"
563
+
552
564
# invalid negative
553
- with pytest .raises (MergeError ):
565
+ with pytest .raises (MergeError , match = msg ):
554
566
merge_asof (
555
567
trades , quotes , on = "time" , by = "ticker" , tolerance = - Timedelta ("1s" )
556
568
)
557
569
558
- with pytest .raises (MergeError ):
570
+ with pytest .raises (MergeError , match = msg ):
559
571
merge_asof (
560
572
trades .reset_index (),
561
573
quotes .reset_index (),
@@ -572,13 +584,13 @@ def test_non_sorted(self):
572
584
# we require that we are already sorted on time & quotes
573
585
assert not trades .time .is_monotonic
574
586
assert not quotes .time .is_monotonic
575
- with pytest .raises (ValueError ):
587
+ with pytest .raises (ValueError , match = "left keys must be sorted" ):
576
588
merge_asof (trades , quotes , on = "time" , by = "ticker" )
577
589
578
590
trades = self .trades .sort_values ("time" )
579
591
assert trades .time .is_monotonic
580
592
assert not quotes .time .is_monotonic
581
- with pytest .raises (ValueError ):
593
+ with pytest .raises (ValueError , match = "right keys must be sorted" ):
582
594
merge_asof (trades , quotes , on = "time" , by = "ticker" )
583
595
584
596
quotes = self .quotes .sort_values ("time" )
0 commit comments