@@ -261,10 +261,9 @@ def test_read_xlrd_Book(self):
261
261
262
262
import xlrd
263
263
264
- pth = '__tmp_excel_read_worksheet__.xls'
265
264
df = self .frame
266
265
267
- with ensure_clean (pth ) as pth :
266
+ with ensure_clean ('.xls' ) as pth :
268
267
df .to_excel (pth , "SheetA" )
269
268
book = xlrd .open_workbook (pth )
270
269
@@ -303,7 +302,7 @@ def test_reader_closes_file(self):
303
302
f = open (pth , 'rb' )
304
303
with ExcelFile (f ) as xlsx :
305
304
# parses okay
306
- df = xlsx .parse ('Sheet1' , index_col = 0 )
305
+ xlsx .parse ('Sheet1' , index_col = 0 )
307
306
308
307
self .assertTrue (f .closed )
309
308
@@ -364,12 +363,12 @@ class ExcelWriterBase(SharedItems):
364
363
# 1. A check_skip function that skips your tests if your writer isn't
365
364
# installed.
366
365
# 2. Add a property ext, which is the file extension that your writer
367
- # writes to.
366
+ # writes to. (needs to start with '.' so it's a valid path)
368
367
# 3. Add a property engine_name, which is the name of the writer class.
369
368
def setUp (self ):
370
369
self .check_skip ()
371
370
super (ExcelWriterBase , self ).setUp ()
372
- self .option_name = 'io.excel.%s.writer' % self .ext
371
+ self .option_name = 'io.excel.%s.writer' % self .ext . strip ( '.' )
373
372
self .prev_engine = get_option (self .option_name )
374
373
set_option (self .option_name , self .engine_name )
375
374
@@ -380,10 +379,7 @@ def test_excel_sheet_by_name_raise(self):
380
379
_skip_if_no_xlrd ()
381
380
import xlrd
382
381
383
- ext = self .ext
384
- pth = os .path .join (self .dirpath , 'testit.{0}' .format (ext ))
385
-
386
- with ensure_clean (pth ) as pth :
382
+ with ensure_clean (self .ext ) as pth :
387
383
gt = DataFrame (np .random .randn (10 , 2 ))
388
384
gt .to_excel (pth )
389
385
xl = ExcelFile (pth )
@@ -394,10 +390,8 @@ def test_excel_sheet_by_name_raise(self):
394
390
395
391
def test_excelwriter_contextmanager (self ):
396
392
_skip_if_no_xlrd ()
397
- ext = self .ext
398
- pth = os .path .join (self .dirpath , 'testit.{0}' .format (ext ))
399
393
400
- with ensure_clean (pth ) as pth :
394
+ with ensure_clean (self . ext ) as pth :
401
395
with ExcelWriter (pth ) as writer :
402
396
self .frame .to_excel (writer , 'Data1' )
403
397
self .frame2 .to_excel (writer , 'Data2' )
@@ -410,10 +404,8 @@ def test_excelwriter_contextmanager(self):
410
404
411
405
def test_roundtrip (self ):
412
406
_skip_if_no_xlrd ()
413
- ext = self .ext
414
- path = '__tmp_to_excel_from_excel__.' + ext
415
407
416
- with ensure_clean (path ) as path :
408
+ with ensure_clean (self . ext ) as path :
417
409
self .frame ['A' ][:5 ] = nan
418
410
419
411
self .frame .to_excel (path , 'test1' )
@@ -446,46 +438,39 @@ def test_roundtrip(self):
446
438
447
439
def test_mixed (self ):
448
440
_skip_if_no_xlrd ()
449
- ext = self .ext
450
- path = '__tmp_to_excel_from_excel_mixed__.' + ext
451
441
452
- with ensure_clean (path ) as path :
442
+ with ensure_clean (self . ext ) as path :
453
443
self .mixed_frame .to_excel (path , 'test1' )
454
444
reader = ExcelFile (path )
455
445
recons = reader .parse ('test1' , index_col = 0 )
456
446
tm .assert_frame_equal (self .mixed_frame , recons )
457
447
458
448
def test_tsframe (self ):
459
449
_skip_if_no_xlrd ()
460
- ext = self .ext
461
- path = '__tmp_to_excel_from_excel_tsframe__.' + ext
462
450
463
451
df = tm .makeTimeDataFrame ()[:5 ]
464
452
465
- with ensure_clean (path ) as path :
453
+ with ensure_clean (self . ext ) as path :
466
454
df .to_excel (path , 'test1' )
467
455
reader = ExcelFile (path )
468
456
recons = reader .parse ('test1' )
469
457
tm .assert_frame_equal (df , recons )
470
458
471
459
def test_basics_with_nan (self ):
472
460
_skip_if_no_xlrd ()
473
- ext = self .ext
474
- path = '__tmp_to_excel_from_excel_int_types__.' + ext
475
- self .frame ['A' ][:5 ] = nan
476
- self .frame .to_excel (path , 'test1' )
477
- self .frame .to_excel (path , 'test1' , cols = ['A' , 'B' ])
478
- self .frame .to_excel (path , 'test1' , header = False )
479
- self .frame .to_excel (path , 'test1' , index = False )
461
+ with ensure_clean (self .ext ) as path :
462
+ self .frame ['A' ][:5 ] = nan
463
+ self .frame .to_excel (path , 'test1' )
464
+ self .frame .to_excel (path , 'test1' , cols = ['A' , 'B' ])
465
+ self .frame .to_excel (path , 'test1' , header = False )
466
+ self .frame .to_excel (path , 'test1' , index = False )
480
467
481
468
def test_int_types (self ):
482
469
_skip_if_no_xlrd ()
483
- ext = self .ext
484
- path = '__tmp_to_excel_from_excel_int_types__.' + ext
485
470
486
471
for np_type in (np .int8 , np .int16 , np .int32 , np .int64 ):
487
472
488
- with ensure_clean (path ) as path :
473
+ with ensure_clean (self . ext ) as path :
489
474
# Test np.int values read come back as int (rather than float
490
475
# which is Excel's format).
491
476
frame = DataFrame (np .random .randint (- 10 , 10 , size = (10 , 2 )),
@@ -505,11 +490,9 @@ def test_int_types(self):
505
490
506
491
def test_float_types (self ):
507
492
_skip_if_no_xlrd ()
508
- ext = self .ext
509
- path = '__tmp_to_excel_from_excel_float_types__.' + ext
510
493
511
494
for np_type in (np .float16 , np .float32 , np .float64 ):
512
- with ensure_clean (path ) as path :
495
+ with ensure_clean (self . ext ) as path :
513
496
# Test np.float values read come back as float.
514
497
frame = DataFrame (np .random .random_sample (10 ), dtype = np_type )
515
498
frame .to_excel (path , 'test1' )
@@ -519,11 +502,9 @@ def test_float_types(self):
519
502
520
503
def test_bool_types (self ):
521
504
_skip_if_no_xlrd ()
522
- ext = self .ext
523
- path = '__tmp_to_excel_from_excel_bool_types__.' + ext
524
505
525
506
for np_type in (np .bool8 , np .bool_ ):
526
- with ensure_clean (path ) as path :
507
+ with ensure_clean (self . ext ) as path :
527
508
# Test np.bool values read come back as float.
528
509
frame = (DataFrame ([1 , 0 , True , False ], dtype = np_type ))
529
510
frame .to_excel (path , 'test1' )
@@ -533,10 +514,8 @@ def test_bool_types(self):
533
514
534
515
def test_sheets (self ):
535
516
_skip_if_no_xlrd ()
536
- ext = self .ext
537
- path = '__tmp_to_excel_from_excel_sheets__.' + ext
538
517
539
- with ensure_clean (path ) as path :
518
+ with ensure_clean (self . ext ) as path :
540
519
self .frame ['A' ][:5 ] = nan
541
520
542
521
self .frame .to_excel (path , 'test1' )
@@ -560,10 +539,8 @@ def test_sheets(self):
560
539
561
540
def test_colaliases (self ):
562
541
_skip_if_no_xlrd ()
563
- ext = self .ext
564
- path = '__tmp_to_excel_from_excel_aliases__.' + ext
565
542
566
- with ensure_clean (path ) as path :
543
+ with ensure_clean (self . ext ) as path :
567
544
self .frame ['A' ][:5 ] = nan
568
545
569
546
self .frame .to_excel (path , 'test1' )
@@ -582,10 +559,8 @@ def test_colaliases(self):
582
559
583
560
def test_roundtrip_indexlabels (self ):
584
561
_skip_if_no_xlrd ()
585
- ext = self .ext
586
- path = '__tmp_to_excel_from_excel_indexlabels__.' + ext
587
562
588
- with ensure_clean (path ) as path :
563
+ with ensure_clean (self . ext ) as path :
589
564
590
565
self .frame ['A' ][:5 ] = nan
591
566
@@ -617,10 +592,7 @@ def test_roundtrip_indexlabels(self):
617
592
frame .index .names = ['test' ]
618
593
self .assertEqual (frame .index .names , recons .index .names )
619
594
620
- # test index_labels in same row as column names
621
- path = '%s.%s' % (tm .rands (10 ), ext )
622
-
623
- with ensure_clean (path ) as path :
595
+ with ensure_clean (self .ext ) as path :
624
596
625
597
self .frame .to_excel (path , 'test1' ,
626
598
cols = ['A' , 'B' , 'C' , 'D' ], index = False )
@@ -636,12 +608,10 @@ def test_roundtrip_indexlabels(self):
636
608
def test_excel_roundtrip_indexname (self ):
637
609
_skip_if_no_xlrd ()
638
610
639
- path = '%s.%s' % (tm .rands (10 ), self .ext )
640
-
641
611
df = DataFrame (np .random .randn (10 , 4 ))
642
612
df .index .name = 'foo'
643
613
644
- with ensure_clean (path ) as path :
614
+ with ensure_clean (self . ext ) as path :
645
615
df .to_excel (path )
646
616
647
617
xf = ExcelFile (path )
@@ -656,7 +626,7 @@ def test_excel_roundtrip_datetime(self):
656
626
# datetime.date, not sure what to test here exactly
657
627
path = '__tmp_excel_roundtrip_datetime__.' + self .ext
658
628
tsf = self .tsframe .copy ()
659
- with ensure_clean (path ) as path :
629
+ with ensure_clean (self . ext ) as path :
660
630
661
631
tsf .index = [x .date () for x in self .tsframe .index ]
662
632
tsf .to_excel (path , 'test1' )
@@ -670,7 +640,7 @@ def test_to_excel_periodindex(self):
670
640
frame = self .tsframe
671
641
xp = frame .resample ('M' , kind = 'period' )
672
642
673
- with ensure_clean (path ) as path :
643
+ with ensure_clean (self . ext ) as path :
674
644
xp .to_excel (path , 'sht1' )
675
645
676
646
reader = ExcelFile (path )
@@ -679,8 +649,6 @@ def test_to_excel_periodindex(self):
679
649
680
650
def test_to_excel_multiindex (self ):
681
651
_skip_if_no_xlrd ()
682
- ext = self .ext
683
- path = '__tmp_to_excel_multiindex__' + ext + '__.' + ext
684
652
685
653
frame = self .frame
686
654
old_index = frame .index
@@ -689,7 +657,7 @@ def test_to_excel_multiindex(self):
689
657
names = ['first' , 'second' ])
690
658
frame .index = new_index
691
659
692
- with ensure_clean (path ) as path :
660
+ with ensure_clean (self . ext ) as path :
693
661
frame .to_excel (path , 'test1' , header = False )
694
662
frame .to_excel (path , 'test1' , cols = ['A' , 'B' ])
695
663
@@ -703,16 +671,14 @@ def test_to_excel_multiindex(self):
703
671
704
672
def test_to_excel_multiindex_dates (self ):
705
673
_skip_if_no_xlrd ()
706
- ext = self .ext
707
- path = '__tmp_to_excel_multiindex_dates__' + ext + '__.' + ext
708
674
709
675
# try multiindex with dates
710
676
tsframe = self .tsframe
711
677
old_index = tsframe .index
712
678
new_index = [old_index , np .arange (len (old_index ))]
713
679
tsframe .index = MultiIndex .from_arrays (new_index )
714
680
715
- with ensure_clean (path ) as path :
681
+ with ensure_clean (self . ext ) as path :
716
682
tsframe .to_excel (path , 'test1' , index_label = ['time' , 'foo' ])
717
683
reader = ExcelFile (path )
718
684
recons = reader .parse ('test1' , index_col = [0 , 1 ])
@@ -736,7 +702,7 @@ def test_to_excel_float_format(self):
736
702
[12.32112 , 123123.2 , 321321.2 ]],
737
703
index = ['A' , 'B' ], columns = ['X' , 'Y' , 'Z' ])
738
704
739
- with ensure_clean (filename ) as filename :
705
+ with ensure_clean (self . ext ) as filename :
740
706
df .to_excel (filename , 'test1' , float_format = '%.2f' )
741
707
742
708
reader = ExcelFile (filename )
@@ -748,21 +714,18 @@ def test_to_excel_float_format(self):
748
714
749
715
def test_to_excel_unicode_filename (self ):
750
716
_skip_if_no_xlrd ()
751
- ext = self .ext
752
- filename = u ('\u0192 u.' ) + ext
753
-
754
- try :
755
- f = open (filename , 'wb' )
756
- except UnicodeEncodeError :
757
- raise nose .SkipTest ('no unicode file names on this system' )
758
- else :
759
- f .close ()
760
-
761
- df = DataFrame ([[0.123456 , 0.234567 , 0.567567 ],
762
- [12.32112 , 123123.2 , 321321.2 ]],
763
- index = ['A' , 'B' ], columns = ['X' , 'Y' , 'Z' ])
717
+ with ensure_clean (u ('\u0192 u.' ) + self .ext ) as filename :
718
+ try :
719
+ f = open (filename , 'wb' )
720
+ except UnicodeEncodeError :
721
+ raise nose .SkipTest ('no unicode file names on this system' )
722
+ else :
723
+ f .close ()
724
+
725
+ df = DataFrame ([[0.123456 , 0.234567 , 0.567567 ],
726
+ [12.32112 , 123123.2 , 321321.2 ]],
727
+ index = ['A' , 'B' ], columns = ['X' , 'Y' , 'Z' ])
764
728
765
- with ensure_clean (filename ) as filename :
766
729
df .to_excel (filename , 'test1' , float_format = '%.2f' )
767
730
768
731
reader = ExcelFile (filename )
@@ -879,10 +842,9 @@ def test_excel_010_hemstring(self):
879
842
# override of #2370 until sorted out in 0.11
880
843
881
844
def roundtrip (df , header = True , parser_hdr = 0 ):
882
- path = '__tmp__test_xl_010_%s__.%s' % (np .random .randint (1 , 10000 ), self .ext )
883
- df .to_excel (path , header = header )
884
845
885
- with ensure_clean (path ) as path :
846
+ with ensure_clean (self .ext ) as path :
847
+ df .to_excel (path , header = header )
886
848
xf = pd .ExcelFile (path )
887
849
res = xf .parse (xf .sheet_names [0 ], header = parser_hdr )
888
850
return res
@@ -926,10 +888,8 @@ def roundtrip(df, header=True, parser_hdr=0):
926
888
def test_duplicated_columns (self ):
927
889
# Test for issue #5235.
928
890
_skip_if_no_xlrd ()
929
- ext = self .ext
930
- path = '__tmp_to_excel_duplicated_columns__.' + ext
931
891
932
- with ensure_clean (path ) as path :
892
+ with ensure_clean (self . ext ) as path :
933
893
write_frame = DataFrame ([[1 , 2 , 3 ], [1 , 2 , 3 ], [1 , 2 , 3 ]])
934
894
colnames = ['A' , 'B' , 'B' ]
935
895
@@ -943,7 +903,7 @@ def test_duplicated_columns(self):
943
903
944
904
945
905
class OpenpyxlTests (ExcelWriterBase , unittest .TestCase ):
946
- ext = 'xlsx'
906
+ ext = '. xlsx'
947
907
engine_name = 'openpyxl'
948
908
check_skip = staticmethod (_skip_if_no_openpyxl )
949
909
@@ -974,7 +934,7 @@ def test_to_excel_styleconverter(self):
974
934
975
935
976
936
class XlwtTests (ExcelWriterBase , unittest .TestCase ):
977
- ext = 'xls'
937
+ ext = '. xls'
978
938
engine_name = 'xlwt'
979
939
check_skip = staticmethod (_skip_if_no_xlwt )
980
940
@@ -999,18 +959,16 @@ def test_to_excel_styleconverter(self):
999
959
1000
960
1001
961
class XlsxWriterTests (ExcelWriterBase , unittest .TestCase ):
1002
- ext = 'xlsx'
962
+ ext = '. xlsx'
1003
963
engine_name = 'xlsxwriter'
1004
964
check_skip = staticmethod (_skip_if_no_xlsxwriter )
1005
965
1006
966
# Override test from the Superclass to use assertAlmostEqual on the
1007
967
# floating point values read back in from the output XlsxWriter file.
1008
968
def test_roundtrip_indexlabels (self ):
1009
969
_skip_if_no_xlrd ()
1010
- ext = self .ext
1011
- path = '__tmp_to_excel_from_excel_indexlabels__.' + ext
1012
970
1013
- with ensure_clean (path ) as path :
971
+ with ensure_clean (self . ext ) as path :
1014
972
1015
973
self .frame ['A' ][:5 ] = nan
1016
974
0 commit comments