@@ -617,6 +617,44 @@ def test_insert(self):
617
617
result = period_range ("2017Q1" , periods = 4 , freq = "Q" ).insert (1 , na )
618
618
tm .assert_index_equal (result , expected )
619
619
620
+ @pytest .mark .parametrize (
621
+ "msg, key" ,
622
+ [
623
+ (r"Period\('2019', 'A-DEC'\), 'foo', 'bar'" , (Period (2019 ), "foo" , "bar" )),
624
+ (r"Period\('2019', 'A-DEC'\), 'y1', 'bar'" , (Period (2019 ), "y1" , "bar" )),
625
+ (r"Period\('2019', 'A-DEC'\), 'foo', 'z1'" , (Period (2019 ), "foo" , "z1" )),
626
+ (
627
+ r"Period\('2018', 'A-DEC'\), Period\('2016', 'A-DEC'\), 'bar'" ,
628
+ (Period (2018 ), Period (2016 ), "bar" ),
629
+ ),
630
+ (r"Period\('2018', 'A-DEC'\), 'foo', 'y1'" , (Period (2018 ), "foo" , "y1" )),
631
+ (
632
+ r"Period\('2017', 'A-DEC'\), 'foo', Period\('2015', 'A-DEC'\)" ,
633
+ (Period (2017 ), "foo" , Period (2015 )),
634
+ ),
635
+ (r"Period\('2017', 'A-DEC'\), 'z1', 'bar'" , (Period (2017 ), "z1" , "bar" )),
636
+ ],
637
+ )
638
+ def test_contains_raise_error_if_period_index_is_in_multi_index (self , msg , key ):
639
+ # issue 20684
640
+ """
641
+ parse_time_string return parameter if type not matched.
642
+ PeriodIndex.get_loc take returned value from parse_time_string as a tuple.
643
+ If first argument is Period and a tuple has 3 items,
644
+ process go on not raise exception
645
+ """
646
+ df = DataFrame (
647
+ {
648
+ "A" : [Period (2019 ), "x1" , "x2" ],
649
+ "B" : [Period (2018 ), Period (2016 ), "y1" ],
650
+ "C" : [Period (2017 ), "z1" , Period (2015 )],
651
+ "V1" : [1 , 2 , 3 ],
652
+ "V2" : [10 , 20 , 30 ],
653
+ }
654
+ ).set_index (["A" , "B" , "C" ])
655
+ with pytest .raises (KeyError , match = msg ):
656
+ df .loc [key ]
657
+
620
658
621
659
def test_maybe_convert_timedelta ():
622
660
pi = PeriodIndex (["2000" , "2001" ], freq = "D" )
0 commit comments