Skip to content

Commit 223d652

Browse files
committed
TST: Use tempfiles in all tests.
If you really need to, possible to opt-out with make_tempfile=False. HDF has trickier requirements regarding files, so it's opting out for now.
1 parent 8828dbf commit 223d652

File tree

5 files changed

+79
-116
lines changed

5 files changed

+79
-116
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ Bug Fixes
769769
- The GroupBy methods ``transform`` and ``filter`` can be used on Series
770770
and DataFrames that have repeated (non-unique) indices. (:issue:`4620`)
771771
- Fix empty series not printing name in repr (:issue:`4651`)
772+
- Make tests create temp files in temp directory by default. (:issue:`5419`)
772773

773774
pandas 0.12.0
774775
-------------

pandas/io/tests/test_excel.py

Lines changed: 46 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,9 @@ def test_read_xlrd_Book(self):
261261

262262
import xlrd
263263

264-
pth = '__tmp_excel_read_worksheet__.xls'
265264
df = self.frame
266265

267-
with ensure_clean(pth) as pth:
266+
with ensure_clean('.xls') as pth:
268267
df.to_excel(pth, "SheetA")
269268
book = xlrd.open_workbook(pth)
270269

@@ -303,7 +302,7 @@ def test_reader_closes_file(self):
303302
f = open(pth, 'rb')
304303
with ExcelFile(f) as xlsx:
305304
# parses okay
306-
df = xlsx.parse('Sheet1', index_col=0)
305+
xlsx.parse('Sheet1', index_col=0)
307306

308307
self.assertTrue(f.closed)
309308

@@ -364,12 +363,12 @@ class ExcelWriterBase(SharedItems):
364363
# 1. A check_skip function that skips your tests if your writer isn't
365364
# installed.
366365
# 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)
368367
# 3. Add a property engine_name, which is the name of the writer class.
369368
def setUp(self):
370369
self.check_skip()
371370
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('.')
373372
self.prev_engine = get_option(self.option_name)
374373
set_option(self.option_name, self.engine_name)
375374

@@ -380,10 +379,7 @@ def test_excel_sheet_by_name_raise(self):
380379
_skip_if_no_xlrd()
381380
import xlrd
382381

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:
387383
gt = DataFrame(np.random.randn(10, 2))
388384
gt.to_excel(pth)
389385
xl = ExcelFile(pth)
@@ -394,10 +390,8 @@ def test_excel_sheet_by_name_raise(self):
394390

395391
def test_excelwriter_contextmanager(self):
396392
_skip_if_no_xlrd()
397-
ext = self.ext
398-
pth = os.path.join(self.dirpath, 'testit.{0}'.format(ext))
399393

400-
with ensure_clean(pth) as pth:
394+
with ensure_clean(self.ext) as pth:
401395
with ExcelWriter(pth) as writer:
402396
self.frame.to_excel(writer, 'Data1')
403397
self.frame2.to_excel(writer, 'Data2')
@@ -410,10 +404,8 @@ def test_excelwriter_contextmanager(self):
410404

411405
def test_roundtrip(self):
412406
_skip_if_no_xlrd()
413-
ext = self.ext
414-
path = '__tmp_to_excel_from_excel__.' + ext
415407

416-
with ensure_clean(path) as path:
408+
with ensure_clean(self.ext) as path:
417409
self.frame['A'][:5] = nan
418410

419411
self.frame.to_excel(path, 'test1')
@@ -446,46 +438,39 @@ def test_roundtrip(self):
446438

447439
def test_mixed(self):
448440
_skip_if_no_xlrd()
449-
ext = self.ext
450-
path = '__tmp_to_excel_from_excel_mixed__.' + ext
451441

452-
with ensure_clean(path) as path:
442+
with ensure_clean(self.ext) as path:
453443
self.mixed_frame.to_excel(path, 'test1')
454444
reader = ExcelFile(path)
455445
recons = reader.parse('test1', index_col=0)
456446
tm.assert_frame_equal(self.mixed_frame, recons)
457447

458448
def test_tsframe(self):
459449
_skip_if_no_xlrd()
460-
ext = self.ext
461-
path = '__tmp_to_excel_from_excel_tsframe__.' + ext
462450

463451
df = tm.makeTimeDataFrame()[:5]
464452

465-
with ensure_clean(path) as path:
453+
with ensure_clean(self.ext) as path:
466454
df.to_excel(path, 'test1')
467455
reader = ExcelFile(path)
468456
recons = reader.parse('test1')
469457
tm.assert_frame_equal(df, recons)
470458

471459
def test_basics_with_nan(self):
472460
_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)
480467

481468
def test_int_types(self):
482469
_skip_if_no_xlrd()
483-
ext = self.ext
484-
path = '__tmp_to_excel_from_excel_int_types__.' + ext
485470

486471
for np_type in (np.int8, np.int16, np.int32, np.int64):
487472

488-
with ensure_clean(path) as path:
473+
with ensure_clean(self.ext) as path:
489474
# Test np.int values read come back as int (rather than float
490475
# which is Excel's format).
491476
frame = DataFrame(np.random.randint(-10, 10, size=(10, 2)),
@@ -505,11 +490,9 @@ def test_int_types(self):
505490

506491
def test_float_types(self):
507492
_skip_if_no_xlrd()
508-
ext = self.ext
509-
path = '__tmp_to_excel_from_excel_float_types__.' + ext
510493

511494
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:
513496
# Test np.float values read come back as float.
514497
frame = DataFrame(np.random.random_sample(10), dtype=np_type)
515498
frame.to_excel(path, 'test1')
@@ -519,11 +502,9 @@ def test_float_types(self):
519502

520503
def test_bool_types(self):
521504
_skip_if_no_xlrd()
522-
ext = self.ext
523-
path = '__tmp_to_excel_from_excel_bool_types__.' + ext
524505

525506
for np_type in (np.bool8, np.bool_):
526-
with ensure_clean(path) as path:
507+
with ensure_clean(self.ext) as path:
527508
# Test np.bool values read come back as float.
528509
frame = (DataFrame([1, 0, True, False], dtype=np_type))
529510
frame.to_excel(path, 'test1')
@@ -533,10 +514,8 @@ def test_bool_types(self):
533514

534515
def test_sheets(self):
535516
_skip_if_no_xlrd()
536-
ext = self.ext
537-
path = '__tmp_to_excel_from_excel_sheets__.' + ext
538517

539-
with ensure_clean(path) as path:
518+
with ensure_clean(self.ext) as path:
540519
self.frame['A'][:5] = nan
541520

542521
self.frame.to_excel(path, 'test1')
@@ -560,10 +539,8 @@ def test_sheets(self):
560539

561540
def test_colaliases(self):
562541
_skip_if_no_xlrd()
563-
ext = self.ext
564-
path = '__tmp_to_excel_from_excel_aliases__.' + ext
565542

566-
with ensure_clean(path) as path:
543+
with ensure_clean(self.ext) as path:
567544
self.frame['A'][:5] = nan
568545

569546
self.frame.to_excel(path, 'test1')
@@ -582,10 +559,8 @@ def test_colaliases(self):
582559

583560
def test_roundtrip_indexlabels(self):
584561
_skip_if_no_xlrd()
585-
ext = self.ext
586-
path = '__tmp_to_excel_from_excel_indexlabels__.' + ext
587562

588-
with ensure_clean(path) as path:
563+
with ensure_clean(self.ext) as path:
589564

590565
self.frame['A'][:5] = nan
591566

@@ -617,10 +592,7 @@ def test_roundtrip_indexlabels(self):
617592
frame.index.names = ['test']
618593
self.assertEqual(frame.index.names, recons.index.names)
619594

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:
624596

625597
self.frame.to_excel(path, 'test1',
626598
cols=['A', 'B', 'C', 'D'], index=False)
@@ -636,12 +608,10 @@ def test_roundtrip_indexlabels(self):
636608
def test_excel_roundtrip_indexname(self):
637609
_skip_if_no_xlrd()
638610

639-
path = '%s.%s' % (tm.rands(10), self.ext)
640-
641611
df = DataFrame(np.random.randn(10, 4))
642612
df.index.name = 'foo'
643613

644-
with ensure_clean(path) as path:
614+
with ensure_clean(self.ext) as path:
645615
df.to_excel(path)
646616

647617
xf = ExcelFile(path)
@@ -656,7 +626,7 @@ def test_excel_roundtrip_datetime(self):
656626
# datetime.date, not sure what to test here exactly
657627
path = '__tmp_excel_roundtrip_datetime__.' + self.ext
658628
tsf = self.tsframe.copy()
659-
with ensure_clean(path) as path:
629+
with ensure_clean(self.ext) as path:
660630

661631
tsf.index = [x.date() for x in self.tsframe.index]
662632
tsf.to_excel(path, 'test1')
@@ -670,7 +640,7 @@ def test_to_excel_periodindex(self):
670640
frame = self.tsframe
671641
xp = frame.resample('M', kind='period')
672642

673-
with ensure_clean(path) as path:
643+
with ensure_clean(self.ext) as path:
674644
xp.to_excel(path, 'sht1')
675645

676646
reader = ExcelFile(path)
@@ -679,8 +649,6 @@ def test_to_excel_periodindex(self):
679649

680650
def test_to_excel_multiindex(self):
681651
_skip_if_no_xlrd()
682-
ext = self.ext
683-
path = '__tmp_to_excel_multiindex__' + ext + '__.' + ext
684652

685653
frame = self.frame
686654
old_index = frame.index
@@ -689,7 +657,7 @@ def test_to_excel_multiindex(self):
689657
names=['first', 'second'])
690658
frame.index = new_index
691659

692-
with ensure_clean(path) as path:
660+
with ensure_clean(self.ext) as path:
693661
frame.to_excel(path, 'test1', header=False)
694662
frame.to_excel(path, 'test1', cols=['A', 'B'])
695663

@@ -703,16 +671,14 @@ def test_to_excel_multiindex(self):
703671

704672
def test_to_excel_multiindex_dates(self):
705673
_skip_if_no_xlrd()
706-
ext = self.ext
707-
path = '__tmp_to_excel_multiindex_dates__' + ext + '__.' + ext
708674

709675
# try multiindex with dates
710676
tsframe = self.tsframe
711677
old_index = tsframe.index
712678
new_index = [old_index, np.arange(len(old_index))]
713679
tsframe.index = MultiIndex.from_arrays(new_index)
714680

715-
with ensure_clean(path) as path:
681+
with ensure_clean(self.ext) as path:
716682
tsframe.to_excel(path, 'test1', index_label=['time', 'foo'])
717683
reader = ExcelFile(path)
718684
recons = reader.parse('test1', index_col=[0, 1])
@@ -736,7 +702,7 @@ def test_to_excel_float_format(self):
736702
[12.32112, 123123.2, 321321.2]],
737703
index=['A', 'B'], columns=['X', 'Y', 'Z'])
738704

739-
with ensure_clean(filename) as filename:
705+
with ensure_clean(self.ext) as filename:
740706
df.to_excel(filename, 'test1', float_format='%.2f')
741707

742708
reader = ExcelFile(filename)
@@ -748,21 +714,18 @@ def test_to_excel_float_format(self):
748714

749715
def test_to_excel_unicode_filename(self):
750716
_skip_if_no_xlrd()
751-
ext = self.ext
752-
filename = u('\u0192u.') + 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('\u0192u.') + 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'])
764728

765-
with ensure_clean(filename) as filename:
766729
df.to_excel(filename, 'test1', float_format='%.2f')
767730

768731
reader = ExcelFile(filename)
@@ -879,10 +842,9 @@ def test_excel_010_hemstring(self):
879842
# override of #2370 until sorted out in 0.11
880843

881844
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)
884845

885-
with ensure_clean(path) as path:
846+
with ensure_clean(self.ext) as path:
847+
df.to_excel(path, header=header)
886848
xf = pd.ExcelFile(path)
887849
res = xf.parse(xf.sheet_names[0], header=parser_hdr)
888850
return res
@@ -926,10 +888,8 @@ def roundtrip(df, header=True, parser_hdr=0):
926888
def test_duplicated_columns(self):
927889
# Test for issue #5235.
928890
_skip_if_no_xlrd()
929-
ext = self.ext
930-
path = '__tmp_to_excel_duplicated_columns__.' + ext
931891

932-
with ensure_clean(path) as path:
892+
with ensure_clean(self.ext) as path:
933893
write_frame = DataFrame([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
934894
colnames = ['A', 'B', 'B']
935895

@@ -943,7 +903,7 @@ def test_duplicated_columns(self):
943903

944904

945905
class OpenpyxlTests(ExcelWriterBase, unittest.TestCase):
946-
ext = 'xlsx'
906+
ext = '.xlsx'
947907
engine_name = 'openpyxl'
948908
check_skip = staticmethod(_skip_if_no_openpyxl)
949909

@@ -974,7 +934,7 @@ def test_to_excel_styleconverter(self):
974934

975935

976936
class XlwtTests(ExcelWriterBase, unittest.TestCase):
977-
ext = 'xls'
937+
ext = '.xls'
978938
engine_name = 'xlwt'
979939
check_skip = staticmethod(_skip_if_no_xlwt)
980940

@@ -999,18 +959,16 @@ def test_to_excel_styleconverter(self):
999959

1000960

1001961
class XlsxWriterTests(ExcelWriterBase, unittest.TestCase):
1002-
ext = 'xlsx'
962+
ext = '.xlsx'
1003963
engine_name = 'xlsxwriter'
1004964
check_skip = staticmethod(_skip_if_no_xlsxwriter)
1005965

1006966
# Override test from the Superclass to use assertAlmostEqual on the
1007967
# floating point values read back in from the output XlsxWriter file.
1008968
def test_roundtrip_indexlabels(self):
1009969
_skip_if_no_xlrd()
1010-
ext = self.ext
1011-
path = '__tmp_to_excel_from_excel_indexlabels__.' + ext
1012970

1013-
with ensure_clean(path) as path:
971+
with ensure_clean(self.ext) as path:
1014972

1015973
self.frame['A'][:5] = nan
1016974

0 commit comments

Comments
 (0)