@@ -342,8 +342,8 @@ def test_excel_sheet_by_name_raise(self, path, engine):
342
342
gt = DataFrame (np .random .randn (10 , 2 ))
343
343
gt .to_excel (path )
344
344
345
- xl = ExcelFile (path )
346
- df = pd .read_excel (xl , sheet_name = 0 , index_col = 0 )
345
+ with ExcelFile (path ) as xl :
346
+ df = pd .read_excel (xl , sheet_name = 0 , index_col = 0 )
347
347
348
348
tm .assert_frame_equal (gt , df )
349
349
@@ -419,8 +419,8 @@ def test_mixed(self, frame, path):
419
419
mixed_frame ["foo" ] = "bar"
420
420
421
421
mixed_frame .to_excel (path , "test1" )
422
- reader = ExcelFile (path )
423
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
422
+ with ExcelFile (path ) as reader :
423
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
424
424
tm .assert_frame_equal (mixed_frame , recons )
425
425
426
426
def test_ts_frame (self , tsframe , path ):
@@ -431,9 +431,8 @@ def test_ts_frame(self, tsframe, path):
431
431
df .index = index
432
432
433
433
df .to_excel (path , "test1" )
434
- reader = ExcelFile (path )
435
-
436
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
434
+ with ExcelFile (path ) as reader :
435
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
437
436
tm .assert_frame_equal (df , recons )
438
437
439
438
def test_basics_with_nan (self , frame , path ):
@@ -451,8 +450,8 @@ def test_int_types(self, np_type, path):
451
450
df = DataFrame (np .random .randint (- 10 , 10 , size = (10 , 2 )), dtype = np_type )
452
451
df .to_excel (path , "test1" )
453
452
454
- reader = ExcelFile (path )
455
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
453
+ with ExcelFile (path ) as reader :
454
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
456
455
457
456
int_frame = df .astype (np .int64 )
458
457
tm .assert_frame_equal (int_frame , recons )
@@ -475,8 +474,10 @@ def test_float_types(self, np_type, path):
475
474
df = DataFrame (np .random .random_sample (10 ), dtype = np_type )
476
475
df .to_excel (path , "test1" )
477
476
478
- reader = ExcelFile (path )
479
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (np_type )
477
+ with ExcelFile (path ) as reader :
478
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (
479
+ np_type
480
+ )
480
481
481
482
tm .assert_frame_equal (df , recons )
482
483
@@ -486,17 +487,19 @@ def test_bool_types(self, np_type, path):
486
487
df = DataFrame ([1 , 0 , True , False ], dtype = np_type )
487
488
df .to_excel (path , "test1" )
488
489
489
- reader = ExcelFile (path )
490
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (np_type )
490
+ with ExcelFile (path ) as reader :
491
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (
492
+ np_type
493
+ )
491
494
492
495
tm .assert_frame_equal (df , recons )
493
496
494
497
def test_inf_roundtrip (self , path ):
495
498
df = DataFrame ([(1 , np .inf ), (2 , 3 ), (5 , - np .inf )])
496
499
df .to_excel (path , "test1" )
497
500
498
- reader = ExcelFile (path )
499
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
501
+ with ExcelFile (path ) as reader :
502
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
500
503
501
504
tm .assert_frame_equal (df , recons )
502
505
@@ -518,11 +521,11 @@ def test_sheets(self, frame, tsframe, path):
518
521
with ExcelWriter (path ) as writer :
519
522
frame .to_excel (writer , "test1" )
520
523
tsframe .to_excel (writer , "test2" )
521
- reader = ExcelFile (path )
522
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
523
- tm .assert_frame_equal (frame , recons )
524
- recons = pd .read_excel (reader , sheet_name = "test2" , index_col = 0 )
525
- tm .assert_frame_equal (tsframe , recons )
524
+ with ExcelFile (path ) as reader :
525
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
526
+ tm .assert_frame_equal (frame , recons )
527
+ recons = pd .read_excel (reader , sheet_name = "test2" , index_col = 0 )
528
+ tm .assert_frame_equal (tsframe , recons )
526
529
assert 2 == len (reader .sheet_names )
527
530
assert "test1" == reader .sheet_names [0 ]
528
531
assert "test2" == reader .sheet_names [1 ]
@@ -539,8 +542,8 @@ def test_colaliases(self, frame, path):
539
542
# column aliases
540
543
col_aliases = Index (["AA" , "X" , "Y" , "Z" ])
541
544
frame .to_excel (path , "test1" , header = col_aliases )
542
- reader = ExcelFile (path )
543
- rs = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
545
+ with ExcelFile (path ) as reader :
546
+ rs = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
544
547
xp = frame .copy ()
545
548
xp .columns = col_aliases
546
549
tm .assert_frame_equal (xp , rs )
@@ -557,8 +560,10 @@ def test_roundtrip_indexlabels(self, merge_cells, frame, path):
557
560
# test index_label
558
561
df = DataFrame (np .random .randn (10 , 2 )) >= 0
559
562
df .to_excel (path , "test1" , index_label = ["test" ], merge_cells = merge_cells )
560
- reader = ExcelFile (path )
561
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (np .int64 )
563
+ with ExcelFile (path ) as reader :
564
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (
565
+ np .int64
566
+ )
562
567
df .index .names = ["test" ]
563
568
assert df .index .names == recons .index .names
564
569
@@ -569,15 +574,19 @@ def test_roundtrip_indexlabels(self, merge_cells, frame, path):
569
574
index_label = ["test" , "dummy" , "dummy2" ],
570
575
merge_cells = merge_cells ,
571
576
)
572
- reader = ExcelFile (path )
573
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (np .int64 )
577
+ with ExcelFile (path ) as reader :
578
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (
579
+ np .int64
580
+ )
574
581
df .index .names = ["test" ]
575
582
assert df .index .names == recons .index .names
576
583
577
584
df = DataFrame (np .random .randn (10 , 2 )) >= 0
578
585
df .to_excel (path , "test1" , index_label = "test" , merge_cells = merge_cells )
579
- reader = ExcelFile (path )
580
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (np .int64 )
586
+ with ExcelFile (path ) as reader :
587
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 ).astype (
588
+ np .int64
589
+ )
581
590
df .index .names = ["test" ]
582
591
tm .assert_frame_equal (df , recons .astype (bool ))
583
592
@@ -592,8 +601,8 @@ def test_roundtrip_indexlabels(self, merge_cells, frame, path):
592
601
df = frame .copy ()
593
602
df = df .set_index (["A" , "B" ])
594
603
595
- reader = ExcelFile (path )
596
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = [0 , 1 ])
604
+ with ExcelFile (path ) as reader :
605
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = [0 , 1 ])
597
606
tm .assert_frame_equal (df , recons )
598
607
599
608
def test_excel_roundtrip_indexname (self , merge_cells , path ):
@@ -602,8 +611,8 @@ def test_excel_roundtrip_indexname(self, merge_cells, path):
602
611
603
612
df .to_excel (path , merge_cells = merge_cells )
604
613
605
- xf = ExcelFile (path )
606
- result = pd .read_excel (xf , sheet_name = xf .sheet_names [0 ], index_col = 0 )
614
+ with ExcelFile (path ) as xf :
615
+ result = pd .read_excel (xf , sheet_name = xf .sheet_names [0 ], index_col = 0 )
607
616
608
617
tm .assert_frame_equal (result , df )
609
618
assert result .index .name == "foo"
@@ -620,8 +629,8 @@ def test_excel_roundtrip_datetime(self, merge_cells, tsframe, path):
620
629
tsf .index = [x .date () for x in tsframe .index ]
621
630
tsf .to_excel (path , "test1" , merge_cells = merge_cells )
622
631
623
- reader = ExcelFile (path )
624
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
632
+ with ExcelFile (path ) as reader :
633
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
625
634
626
635
tm .assert_frame_equal (tsframe , recons )
627
636
@@ -680,9 +689,8 @@ def test_to_excel_interval_no_labels(self, path):
680
689
expected ["new" ] = pd .cut (expected [0 ], 10 ).astype (str )
681
690
682
691
df .to_excel (path , "test1" )
683
- reader = ExcelFile (path )
684
-
685
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
692
+ with ExcelFile (path ) as reader :
693
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
686
694
tm .assert_frame_equal (expected , recons )
687
695
688
696
def test_to_excel_interval_labels (self , path ):
@@ -698,9 +706,8 @@ def test_to_excel_interval_labels(self, path):
698
706
expected ["new" ] = pd .Series (list (intervals ))
699
707
700
708
df .to_excel (path , "test1" )
701
- reader = ExcelFile (path )
702
-
703
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
709
+ with ExcelFile (path ) as reader :
710
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
704
711
tm .assert_frame_equal (expected , recons )
705
712
706
713
def test_to_excel_timedelta (self , path ):
@@ -718,18 +725,17 @@ def test_to_excel_timedelta(self, path):
718
725
)
719
726
720
727
df .to_excel (path , "test1" )
721
- reader = ExcelFile (path )
722
-
723
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
728
+ with ExcelFile (path ) as reader :
729
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
724
730
tm .assert_frame_equal (expected , recons )
725
731
726
732
def test_to_excel_periodindex (self , tsframe , path ):
727
733
xp = tsframe .resample ("M" , kind = "period" ).mean ()
728
734
729
735
xp .to_excel (path , "sht1" )
730
736
731
- reader = ExcelFile (path )
732
- rs = pd .read_excel (reader , sheet_name = "sht1" , index_col = 0 )
737
+ with ExcelFile (path ) as reader :
738
+ rs = pd .read_excel (reader , sheet_name = "sht1" , index_col = 0 )
733
739
tm .assert_frame_equal (xp , rs .to_period ("M" ))
734
740
735
741
def test_to_excel_multiindex (self , merge_cells , frame , path ):
@@ -742,8 +748,8 @@ def test_to_excel_multiindex(self, merge_cells, frame, path):
742
748
743
749
# round trip
744
750
frame .to_excel (path , "test1" , merge_cells = merge_cells )
745
- reader = ExcelFile (path )
746
- df = pd .read_excel (reader , sheet_name = "test1" , index_col = [0 , 1 ])
751
+ with ExcelFile (path ) as reader :
752
+ df = pd .read_excel (reader , sheet_name = "test1" , index_col = [0 , 1 ])
747
753
tm .assert_frame_equal (frame , df )
748
754
749
755
# GH13511
@@ -771,8 +777,10 @@ def test_to_excel_multiindex_cols(self, merge_cells, frame, path):
771
777
772
778
# round trip
773
779
frame .to_excel (path , "test1" , merge_cells = merge_cells )
774
- reader = ExcelFile (path )
775
- df = pd .read_excel (reader , sheet_name = "test1" , header = header , index_col = [0 , 1 ])
780
+ with ExcelFile (path ) as reader :
781
+ df = pd .read_excel (
782
+ reader , sheet_name = "test1" , header = header , index_col = [0 , 1 ]
783
+ )
776
784
if not merge_cells :
777
785
fm = frame .columns .format (sparsify = False , adjoin = False , names = False )
778
786
frame .columns = ["." .join (map (str , q )) for q in zip (* fm )]
@@ -785,8 +793,8 @@ def test_to_excel_multiindex_dates(self, merge_cells, tsframe, path):
785
793
786
794
tsframe .index .names = ["time" , "foo" ]
787
795
tsframe .to_excel (path , "test1" , merge_cells = merge_cells )
788
- reader = ExcelFile (path )
789
- recons = pd .read_excel (reader , sheet_name = "test1" , index_col = [0 , 1 ])
796
+ with ExcelFile (path ) as reader :
797
+ recons = pd .read_excel (reader , sheet_name = "test1" , index_col = [0 , 1 ])
790
798
791
799
tm .assert_frame_equal (tsframe , recons )
792
800
assert recons .index .names == ("time" , "foo" )
@@ -806,8 +814,8 @@ def test_to_excel_multiindex_no_write_index(self, path):
806
814
frame2 .to_excel (path , "test1" , index = False )
807
815
808
816
# Read it back in.
809
- reader = ExcelFile (path )
810
- frame3 = pd .read_excel (reader , sheet_name = "test1" )
817
+ with ExcelFile (path ) as reader :
818
+ frame3 = pd .read_excel (reader , sheet_name = "test1" )
811
819
812
820
# Test that it is the same as the initial frame.
813
821
tm .assert_frame_equal (frame1 , frame3 )
@@ -820,8 +828,8 @@ def test_to_excel_float_format(self, path):
820
828
)
821
829
df .to_excel (path , "test1" , float_format = "%.2f" )
822
830
823
- reader = ExcelFile (path )
824
- result = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
831
+ with ExcelFile (path ) as reader :
832
+ result = pd .read_excel (reader , sheet_name = "test1" , index_col = 0 )
825
833
826
834
expected = DataFrame (
827
835
[[0.12 , 0.23 , 0.57 ], [12.32 , 123123.20 , 321321.20 ]],
@@ -979,8 +987,10 @@ def test_excel_010_hemstring(
979
987
def roundtrip (data , header = True , parser_hdr = 0 , index = True ):
980
988
data .to_excel (path , header = header , merge_cells = merge_cells , index = index )
981
989
982
- xf = ExcelFile (path )
983
- return pd .read_excel (xf , sheet_name = xf .sheet_names [0 ], header = parser_hdr )
990
+ with ExcelFile (path ) as xf :
991
+ return pd .read_excel (
992
+ xf , sheet_name = xf .sheet_names [0 ], header = parser_hdr
993
+ )
984
994
985
995
# Basic test.
986
996
parser_header = 0 if use_headers else None
0 commit comments