@@ -665,6 +665,16 @@ def test_bad_engine_raises(self, read_ext):
665665 with pytest .raises (ValueError , match = "Unknown engine: foo" ):
666666 pd .read_excel ("" , engine = bad_engine )
667667
668+ @pytest .mark .parametrize (
669+ "sheet_name" ,
670+ [3 , [0 , 3 ], [3 , 0 ], "Sheet4" , ["Sheet1" , "Sheet4" ], ["Sheet4" , "Sheet1" ]],
671+ )
672+ def test_bad_sheetname_raises (self , read_ext , sheet_name ):
673+ # GH 39250
674+ msg = "Worksheet index 3 is invalid|Worksheet named 'Sheet4' not found"
675+ with pytest .raises (ValueError , match = msg ):
676+ pd .read_excel ("blank" + read_ext , sheet_name = sheet_name )
677+
668678 def test_missing_file_raises (self , read_ext ):
669679 bad_file = f"foo{ read_ext } "
670680 # CI tests with zh_CN.utf8, translates to "No such file or directory"
@@ -1263,6 +1273,17 @@ def test_sheet_name(self, request, read_ext, df_ref):
12631273 tm .assert_frame_equal (df1_parse , df_ref , check_names = False )
12641274 tm .assert_frame_equal (df2_parse , df_ref , check_names = False )
12651275
1276+ @pytest .mark .parametrize (
1277+ "sheet_name" ,
1278+ [3 , [0 , 3 ], [3 , 0 ], "Sheet4" , ["Sheet1" , "Sheet4" ], ["Sheet4" , "Sheet1" ]],
1279+ )
1280+ def test_bad_sheetname_raises (self , read_ext , sheet_name ):
1281+ # GH 39250
1282+ msg = "Worksheet index 3 is invalid|Worksheet named 'Sheet4' not found"
1283+ with pytest .raises (ValueError , match = msg ):
1284+ with pd .ExcelFile ("blank" + read_ext ) as excel :
1285+ excel .parse (sheet_name = sheet_name )
1286+
12661287 def test_excel_read_buffer (self , engine , read_ext ):
12671288 pth = "test1" + read_ext
12681289 expected = pd .read_excel (pth , sheet_name = "Sheet1" , index_col = 0 , engine = engine )
0 commit comments