@@ -111,7 +111,11 @@ def test_per_axis_per_level_getitem(self):
111
111
expected = df .iloc [[2 , 3 ]]
112
112
tm .assert_frame_equal (result , expected )
113
113
114
- with pytest .raises (ValueError ):
114
+ msg = (
115
+ "cannot index with a boolean indexer "
116
+ "that is not the same length as the index"
117
+ )
118
+ with pytest .raises (ValueError , match = msg ):
115
119
df .loc [(slice (None ), np .array ([True , False ])), :]
116
120
117
121
# ambiguous notation
@@ -411,7 +415,11 @@ def test_per_axis_per_level_doc_examples(self):
411
415
tm .assert_frame_equal (result , expected )
412
416
413
417
# not sorted
414
- with pytest .raises (UnsortedIndexError ):
418
+ msg = (
419
+ "MultiIndex slicing requires the index to be lexsorted: "
420
+ r"slicing on levels \[1\], lexsort depth 1"
421
+ )
422
+ with pytest .raises (UnsortedIndexError , match = msg ):
415
423
df .loc ["A1" , ("a" , slice ("foo" ))]
416
424
417
425
# GH 16734: not sorted, but no real slicing
@@ -480,14 +488,10 @@ def test_loc_axis_arguments(self):
480
488
tm .assert_frame_equal (result , expected )
481
489
482
490
# invalid axis
483
- with pytest .raises (ValueError ):
484
- df .loc (axis = - 1 )[:, :, ["C1" , "C3" ]]
485
-
486
- with pytest .raises (ValueError ):
487
- df .loc (axis = 2 )[:, :, ["C1" , "C3" ]]
488
-
489
- with pytest .raises (ValueError ):
490
- df .loc (axis = "foo" )[:, :, ["C1" , "C3" ]]
491
+ for i in [- 1 , 2 , "foo" ]:
492
+ msg = f"No axis named { i } for object type DataFrame"
493
+ with pytest .raises (ValueError , match = msg ):
494
+ df .loc (axis = i )[:, :, ["C1" , "C3" ]]
491
495
492
496
def test_loc_axis_single_level_multi_col_indexing_multiindex_col_df (self ):
493
497
@@ -628,12 +632,14 @@ def test_per_axis_per_level_setitem(self):
628
632
# not enough values
629
633
df = df_orig .copy ()
630
634
631
- with pytest .raises (ValueError ):
635
+ msg = "setting an array element with a sequence."
636
+ with pytest .raises (ValueError , match = msg ):
632
637
df .loc [(slice (None ), 1 ), (slice (None ), ["foo" ])] = np .array (
633
638
[[100 ], [100 , 100 ]], dtype = "int64"
634
639
)
635
640
636
- with pytest .raises (ValueError ):
641
+ msg = "Must have equal len keys and value when setting with an iterable"
642
+ with pytest .raises (ValueError , match = msg ):
637
643
df .loc [(slice (None ), 1 ), (slice (None ), ["foo" ])] = np .array (
638
644
[100 , 100 , 100 , 100 ], dtype = "int64"
639
645
)
0 commit comments