@@ -522,3 +522,47 @@ def test_loc_with_mi_indexer():
522
522
columns = ["author" , "price" ],
523
523
)
524
524
tm .assert_frame_equal (result , expected )
525
+
526
+
527
+ def test_getitem_str_slice (datapath ):
528
+ # GH#15928
529
+ path = datapath ("reshape" , "merge" , "data" , "quotes2.csv" )
530
+ df = pd .read_csv (path , parse_dates = ["time" ])
531
+ df2 = df .set_index (["ticker" , "time" ]).sort_index ()
532
+
533
+ res = df2 .loc [("AAPL" , slice ("2016-05-25 13:30:00" )), :].droplevel (0 )
534
+ expected = df2 .loc ["AAPL" ].loc [slice ("2016-05-25 13:30:00" ), :]
535
+ tm .assert_frame_equal (res , expected )
536
+
537
+
538
+ def test_3levels_leading_period_index ():
539
+ # GH#24091
540
+ pi = pd .PeriodIndex (
541
+ ["20181101 1100" , "20181101 1200" , "20181102 1300" , "20181102 1400" ],
542
+ name = "datetime" ,
543
+ freq = "B" ,
544
+ )
545
+ lev2 = ["A" , "A" , "Z" , "W" ]
546
+ lev3 = ["B" , "C" , "Q" , "F" ]
547
+ mi = pd .MultiIndex .from_arrays ([pi , lev2 , lev3 ])
548
+
549
+ ser = pd .Series (range (4 ), index = mi , dtype = np .float64 )
550
+ result = ser .loc [(pi [0 ], "A" , "B" )]
551
+ assert result == 0.0
552
+
553
+
554
+ class TestKeyErrorsWithMultiIndex :
555
+ def test_missing_keys_raises_keyerror (self ):
556
+ # GH#27420 KeyError, not TypeError
557
+ df = pd .DataFrame (np .arange (12 ).reshape (4 , 3 ), columns = ["A" , "B" , "C" ])
558
+ df2 = df .set_index (["A" , "B" ])
559
+
560
+ with pytest .raises (KeyError , match = "1" ):
561
+ df2 .loc [(1 , 6 )]
562
+
563
+ def test_missing_key_raises_keyerror2 (self ):
564
+ # GH#21168 KeyError, not "IndexingError: Too many indexers"
565
+ ser = pd .Series (- 1 , index = pd .MultiIndex .from_product ([[0 , 1 ]] * 2 ))
566
+
567
+ with pytest .raises (KeyError , match = r"\(0, 3\)" ):
568
+ ser .loc [0 , 3 ]
0 commit comments