@@ -522,10 +522,9 @@ def test_sheets(self, frame, tsframe, path):
522522 frame .to_excel (path , "test1" , index = False )
523523
524524 # Test writing to separate sheets
525- writer = ExcelWriter (path )
526- frame .to_excel (writer , "test1" )
527- tsframe .to_excel (writer , "test2" )
528- writer .close ()
525+ with ExcelWriter (path ) as writer :
526+ frame .to_excel (writer , "test1" )
527+ tsframe .to_excel (writer , "test2" )
529528 reader = ExcelFile (path )
530529 recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
531530 tm .assert_frame_equal (frame , recons )
@@ -1199,17 +1198,16 @@ def test_datetimes(self, path):
11991198
12001199 def test_bytes_io (self , engine ):
12011200 # see gh-7074
1202- bio = BytesIO ()
1203- df = DataFrame (np .random .randn (10 , 2 ))
1201+ with BytesIO () as bio :
1202+ df = DataFrame (np .random .randn (10 , 2 ))
12041203
1205- # Pass engine explicitly, as there is no file path to infer from.
1206- writer = ExcelWriter (bio , engine = engine )
1207- df .to_excel (writer )
1208- writer .save ()
1204+ # Pass engine explicitly, as there is no file path to infer from.
1205+ with ExcelWriter (bio , engine = engine ) as writer :
1206+ df .to_excel (writer )
12091207
1210- bio .seek (0 )
1211- reread_df = pd .read_excel (bio , index_col = 0 )
1212- tm .assert_frame_equal (df , reread_df )
1208+ bio .seek (0 )
1209+ reread_df = pd .read_excel (bio , index_col = 0 )
1210+ tm .assert_frame_equal (df , reread_df )
12131211
12141212 def test_write_lists_dict (self , path ):
12151213 # see gh-8188.
@@ -1317,12 +1315,12 @@ class TestExcelWriterEngineTests:
13171315 )
13181316 def test_ExcelWriter_dispatch (self , klass , ext ):
13191317 with tm .ensure_clean (ext ) as path :
1320- writer = ExcelWriter (path )
1321- if ext == ".xlsx" and td .safe_import ("xlsxwriter" ):
1322- # xlsxwriter has preference over openpyxl if both installed
1323- assert isinstance (writer , _XlsxWriter )
1324- else :
1325- assert isinstance (writer , klass )
1318+ with ExcelWriter (path ) as writer :
1319+ if ext == ".xlsx" and td .safe_import ("xlsxwriter" ):
1320+ # xlsxwriter has preference over openpyxl if both installed
1321+ assert isinstance (writer , _XlsxWriter )
1322+ else :
1323+ assert isinstance (writer , klass )
13261324
13271325 def test_ExcelWriter_dispatch_raises (self ):
13281326 with pytest .raises (ValueError , match = "No engine" ):
@@ -1356,8 +1354,8 @@ def check_called(func):
13561354 path = "something.xlsx"
13571355 with tm .ensure_clean (path ) as filepath :
13581356 register_writer (DummyClass )
1359- writer = ExcelWriter (filepath )
1360- assert isinstance (writer , DummyClass )
1357+ with ExcelWriter (filepath ) as writer :
1358+ assert isinstance (writer , DummyClass )
13611359 df = tm .makeCustomDataframe (1 , 1 )
13621360 check_called (lambda : df .to_excel (filepath ))
13631361 with tm .ensure_clean ("something.xls" ) as filepath :
@@ -1377,5 +1375,5 @@ def test_excelfile_fspath(self):
13771375
13781376 def test_excelwriter_fspath (self ):
13791377 with tm .ensure_clean ("foo.xlsx" ) as path :
1380- writer = ExcelWriter (path )
1381- assert os .fspath (writer ) == str (path )
1378+ with ExcelWriter (path ) as writer :
1379+ assert os .fspath (writer ) == str (path )
0 commit comments