@@ -622,6 +622,16 @@ def test_bad_engine_raises(self, read_ext):
622
622
with pytest .raises (ValueError , match = "Unknown engine: foo" ):
623
623
pd .read_excel ("" , engine = bad_engine )
624
624
625
+ @pytest .mark .parametrize (
626
+ "sheet_name" ,
627
+ [3 , [0 , 3 ], [3 , 0 ], "Sheet4" , ["Sheet1" , "Sheet4" ], ["Sheet4" , "Sheet1" ]],
628
+ )
629
+ def test_bad_sheetname_raises (self , read_ext , sheet_name ):
630
+ # GH 39250
631
+ msg = "Worksheet index 3 is invalid|Worksheet named 'Sheet4' not found"
632
+ with pytest .raises (ValueError , match = msg ):
633
+ pd .read_excel ("blank" + read_ext , sheet_name = sheet_name )
634
+
625
635
def test_missing_file_raises (self , read_ext ):
626
636
bad_file = f"foo{ read_ext } "
627
637
# CI tests with zh_CN.utf8, translates to "No such file or directory"
@@ -1159,6 +1169,17 @@ def test_sheet_name(self, read_ext, df_ref):
1159
1169
tm .assert_frame_equal (df1_parse , df_ref , check_names = False )
1160
1170
tm .assert_frame_equal (df2_parse , df_ref , check_names = False )
1161
1171
1172
+ @pytest .mark .parametrize (
1173
+ "sheet_name" ,
1174
+ [3 , [0 , 3 ], [3 , 0 ], "Sheet4" , ["Sheet1" , "Sheet4" ], ["Sheet4" , "Sheet1" ]],
1175
+ )
1176
+ def test_bad_sheetname_raises (self , read_ext , sheet_name ):
1177
+ # GH 39250
1178
+ msg = "Worksheet index 3 is invalid|Worksheet named 'Sheet4' not found"
1179
+ with pytest .raises (ValueError , match = msg ):
1180
+ with pd .ExcelFile ("blank" + read_ext ) as excel :
1181
+ excel .parse (sheet_name = sheet_name )
1182
+
1162
1183
def test_excel_read_buffer (self , engine , read_ext ):
1163
1184
pth = "test1" + read_ext
1164
1185
expected = pd .read_excel (pth , sheet_name = "Sheet1" , index_col = 0 , engine = engine )
0 commit comments