3636import pandas ._testing as tm
3737from pandas .tests .io .pytables .common import (
3838 _maybe_remove ,
39- create_tempfile ,
4039 ensure_clean_path ,
4140 ensure_clean_store ,
4241 safe_close ,
43- safe_remove ,
4442 tables ,
4543)
4644
@@ -80,33 +78,25 @@ def test_format_kwarg_in_constructor(self, setup_path):
8078
8179 msg = "format is not a defined argument for HDFStore"
8280
83- with ensure_clean_path (setup_path ) as path :
81+ with tm . ensure_clean (setup_path ) as path :
8482 with pytest .raises (ValueError , match = msg ):
8583 HDFStore (path , format = "table" )
8684
8785 def test_context (self , setup_path ):
88- path = create_tempfile (setup_path )
89- try :
90- with HDFStore (path ) as tbl :
91- raise ValueError ("blah" )
92- except ValueError :
93- pass
94- finally :
95- safe_remove (path )
96-
97- try :
86+ with tm .ensure_clean (setup_path ) as path :
87+ try :
88+ with HDFStore (path ) as tbl :
89+ raise ValueError ("blah" )
90+ except ValueError :
91+ pass
92+ with tm .ensure_clean (setup_path ) as path :
9893 with HDFStore (path ) as tbl :
9994 tbl ["a" ] = tm .makeDataFrame ()
100-
101- with HDFStore (path ) as tbl :
10295 assert len (tbl ) == 1
10396 assert type (tbl ["a" ]) == DataFrame
104- finally :
105- safe_remove (path )
10697
10798 def test_conv_read_write (self , setup_path ):
108- path = create_tempfile (setup_path )
109- try :
99+ with tm .ensure_clean () as path :
110100
111101 def roundtrip (key , obj , ** kwargs ):
112102 obj .to_hdf (path , key , ** kwargs )
@@ -127,9 +117,6 @@ def roundtrip(key, obj, **kwargs):
127117 result = read_hdf (path , "table" , where = ["index>2" ])
128118 tm .assert_frame_equal (df [df .index > 2 ], result )
129119
130- finally :
131- safe_remove (path )
132-
133120 def test_long_strings (self , setup_path ):
134121
135122 # GH6166
@@ -605,7 +592,7 @@ def test_reopen_handle(self, setup_path):
605592
606593 def test_open_args (self , setup_path ):
607594
608- with ensure_clean_path (setup_path ) as path :
595+ with tm . ensure_clean (setup_path ) as path :
609596
610597 df = tm .makeDataFrame ()
611598
@@ -621,8 +608,8 @@ def test_open_args(self, setup_path):
621608
622609 store .close ()
623610
624- # the file should not have actually been written
625- assert not os .path .exists (path )
611+ # the file should not have actually been written
612+ assert not os .path .exists (path )
626613
627614 def test_flush (self , setup_path ):
628615
@@ -4194,7 +4181,6 @@ def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs):
41944181 import tempfile
41954182
41964183 fd , new_f = tempfile .mkstemp ()
4197-
41984184 tstore = store .copy (
41994185 new_f , keys = keys , propindexes = propindexes , ** kwargs
42004186 )
@@ -4225,20 +4211,17 @@ def do_copy(f, new_f=None, keys=None, propindexes=True, **kwargs):
42254211 os .close (fd )
42264212 except (OSError , ValueError ):
42274213 pass
4228- safe_remove (new_f )
4214+ os . remove (new_f )
42294215
42304216 # new table
42314217 df = tm .makeDataFrame ()
42324218
4233- try :
4234- path = create_tempfile (setup_path )
4219+ with tm .ensure_clean () as path :
42354220 st = HDFStore (path )
42364221 st .append ("df" , df , data_columns = ["A" ])
42374222 st .close ()
42384223 do_copy (f = path )
42394224 do_copy (f = path , propindexes = False )
4240- finally :
4241- safe_remove (path )
42424225
42434226 def test_store_datetime_fractional_secs (self , setup_path ):
42444227
@@ -4677,7 +4660,7 @@ def test_read_hdf_generic_buffer_errors(self):
46774660
46784661 def test_invalid_complib (self , setup_path ):
46794662 df = DataFrame (np .random .rand (4 , 5 ), index = list ("abcd" ), columns = list ("ABCDE" ))
4680- with ensure_clean_path (setup_path ) as path :
4663+ with tm . ensure_clean (setup_path ) as path :
46814664 with pytest .raises (ValueError ):
46824665 df .to_hdf (path , "df" , complib = "foolib" )
46834666
0 commit comments