@@ -649,6 +649,44 @@ def test_insert(self):
649
649
result = period_range ("2017Q1" , periods = 4 , freq = "Q" ).insert (1 , na )
650
650
tm .assert_index_equal (result , expected )
651
651
652
+ @pytest .mark .parametrize (
653
+ "msg, key" ,
654
+ [
655
+ (r"Period\('2019', 'A-DEC'\), 'foo', 'bar'" , (Period (2019 ), "foo" , "bar" )),
656
+ (r"Period\('2019', 'A-DEC'\), 'y1', 'bar'" , (Period (2019 ), "y1" , "bar" )),
657
+ (r"Period\('2019', 'A-DEC'\), 'foo', 'z1'" , (Period (2019 ), "foo" , "z1" )),
658
+ (
659
+ r"Period\('2018', 'A-DEC'\), Period\('2016', 'A-DEC'\), 'bar'" ,
660
+ (Period (2018 ), Period (2016 ), "bar" ),
661
+ ),
662
+ (r"Period\('2018', 'A-DEC'\), 'foo', 'y1'" , (Period (2018 ), "foo" , "y1" )),
663
+ (
664
+ r"Period\('2017', 'A-DEC'\), 'foo', Period\('2015', 'A-DEC'\)" ,
665
+ (Period (2017 ), "foo" , Period (2015 )),
666
+ ),
667
+ (r"Period\('2017', 'A-DEC'\), 'z1', 'bar'" , (Period (2017 ), "z1" , "bar" )),
668
+ ],
669
+ )
670
+ def test_contains_raise_error_if_period_index_is_in_multi_index (self , msg , key ):
671
+ # issue 20684
672
+ """
673
+ parse_time_string return parameter if type not matched.
674
+ PeriodIndex.get_loc takes returned value from parse_time_string as a tuple.
675
+ If first argument is Period and a tuple has 3 items,
676
+ process go on not raise exception
677
+ """
678
+ df = DataFrame (
679
+ {
680
+ "A" : [Period (2019 ), "x1" , "x2" ],
681
+ "B" : [Period (2018 ), Period (2016 ), "y1" ],
682
+ "C" : [Period (2017 ), "z1" , Period (2015 )],
683
+ "V1" : [1 , 2 , 3 ],
684
+ "V2" : [10 , 20 , 30 ],
685
+ }
686
+ ).set_index (["A" , "B" , "C" ])
687
+ with pytest .raises (KeyError , match = msg ):
688
+ df .loc [key ]
689
+
652
690
653
691
def test_maybe_convert_timedelta ():
654
692
pi = PeriodIndex (["2000" , "2001" ], freq = "D" )
0 commit comments