@@ -522,10 +522,9 @@ def test_sheets(self, frame, tsframe, path):
522
522
frame .to_excel (path , "test1" , index = False )
523
523
524
524
# 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" )
529
528
reader = ExcelFile (path )
530
529
recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
531
530
tm .assert_frame_equal (frame , recons )
@@ -1199,17 +1198,16 @@ def test_datetimes(self, path):
1199
1198
1200
1199
def test_bytes_io (self , engine ):
1201
1200
# 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 ))
1204
1203
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 )
1209
1207
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 )
1213
1211
1214
1212
def test_write_lists_dict (self , path ):
1215
1213
# see gh-8188.
@@ -1317,12 +1315,12 @@ class TestExcelWriterEngineTests:
1317
1315
)
1318
1316
def test_ExcelWriter_dispatch (self , klass , ext ):
1319
1317
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 )
1326
1324
1327
1325
def test_ExcelWriter_dispatch_raises (self ):
1328
1326
with pytest .raises (ValueError , match = "No engine" ):
@@ -1356,8 +1354,8 @@ def check_called(func):
1356
1354
path = "something.xlsx"
1357
1355
with tm .ensure_clean (path ) as filepath :
1358
1356
register_writer (DummyClass )
1359
- writer = ExcelWriter (filepath )
1360
- assert isinstance (writer , DummyClass )
1357
+ with ExcelWriter (filepath ) as writer :
1358
+ assert isinstance (writer , DummyClass )
1361
1359
df = tm .makeCustomDataframe (1 , 1 )
1362
1360
check_called (lambda : df .to_excel (filepath ))
1363
1361
with tm .ensure_clean ("something.xls" ) as filepath :
@@ -1377,5 +1375,5 @@ def test_excelfile_fspath(self):
1377
1375
1378
1376
def test_excelwriter_fspath (self ):
1379
1377
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