26
26
27
27
@pytest .mark .single_cpu
28
28
class TestFeather :
29
- def check_error_on_write (self , df , exc , err_msg ):
29
+ def check_error_on_write (self , df , exc , err_msg , temp_file ):
30
30
# check that we are raising the exception
31
31
# on writing
32
32
33
33
with pytest .raises (exc , match = err_msg ):
34
- with tm . ensure_clean () as path :
35
- to_feather (df , path )
34
+ path = str ( temp_file )
35
+ to_feather (df , path )
36
36
37
- def check_external_error_on_write (self , df ):
37
+ def check_external_error_on_write (self , df , temp_file ):
38
38
# check that we are raising the exception
39
39
# on writing
40
40
41
41
with tm .external_error_raised (Exception ):
42
- with tm . ensure_clean () as path :
43
- to_feather (df , path )
42
+ path = str ( temp_file )
43
+ to_feather (df , path )
44
44
45
- def check_round_trip (self , df , expected = None , write_kwargs = None , ** read_kwargs ):
45
+ def check_round_trip (
46
+ self , df , temp_file , expected = None , write_kwargs = None , ** read_kwargs
47
+ ):
46
48
if write_kwargs is None :
47
49
write_kwargs = {}
48
50
if expected is None :
49
51
expected = df .copy ()
50
52
51
- with tm . ensure_clean () as path :
52
- to_feather (df , path , ** write_kwargs )
53
+ path = str ( temp_file )
54
+ to_feather (df , path , ** write_kwargs )
53
55
54
- result = read_feather (path , ** read_kwargs )
56
+ result = read_feather (path , ** read_kwargs )
55
57
56
- tm .assert_frame_equal (result , expected )
58
+ tm .assert_frame_equal (result , expected )
57
59
58
- def test_error (self ):
60
+ def test_error (self , temp_file ):
59
61
msg = "feather only support IO with DataFrames"
60
62
for obj in [
61
63
pd .Series ([1 , 2 , 3 ]),
@@ -64,9 +66,9 @@ def test_error(self):
64
66
pd .Timestamp ("20130101" ),
65
67
np .array ([1 , 2 , 3 ]),
66
68
]:
67
- self .check_error_on_write (obj , ValueError , msg )
69
+ self .check_error_on_write (obj , ValueError , msg , temp_file )
68
70
69
- def test_basic (self ):
71
+ def test_basic (self , temp_file ):
70
72
tz = zoneinfo .ZoneInfo ("US/Eastern" )
71
73
df = pd .DataFrame (
72
74
{
@@ -103,15 +105,15 @@ def test_basic(self):
103
105
104
106
expected = df .copy ()
105
107
expected .loc [1 , "bool_with_null" ] = None
106
- self .check_round_trip (df , expected = expected )
108
+ self .check_round_trip (df , temp_file , expected = expected )
107
109
108
- def test_duplicate_columns (self ):
110
+ def test_duplicate_columns (self , temp_file ):
109
111
# https://github.com/wesm/feather/issues/53
110
112
# not currently able to handle duplicate columns
111
113
df = pd .DataFrame (np .arange (12 ).reshape (4 , 3 ), columns = list ("aaa" )).copy ()
112
- self .check_external_error_on_write (df )
114
+ self .check_external_error_on_write (df , temp_file )
113
115
114
- def test_read_columns (self ):
116
+ def test_read_columns (self , temp_file ):
115
117
# GH 24025
116
118
df = pd .DataFrame (
117
119
{
@@ -122,23 +124,23 @@ def test_read_columns(self):
122
124
}
123
125
)
124
126
columns = ["col1" , "col3" ]
125
- self .check_round_trip (df , expected = df [columns ], columns = columns )
127
+ self .check_round_trip (df , temp_file , expected = df [columns ], columns = columns )
126
128
127
- def test_read_columns_different_order (self ):
129
+ def test_read_columns_different_order (self , temp_file ):
128
130
# GH 33878
129
131
df = pd .DataFrame ({"A" : [1 , 2 ], "B" : ["x" , "y" ], "C" : [True , False ]})
130
132
expected = df [["B" , "A" ]]
131
- self .check_round_trip (df , expected , columns = ["B" , "A" ])
133
+ self .check_round_trip (df , temp_file , expected , columns = ["B" , "A" ])
132
134
133
- def test_unsupported_other (self ):
135
+ def test_unsupported_other (self , temp_file ):
134
136
# mixed python objects
135
137
df = pd .DataFrame ({"a" : ["a" , 1 , 2.0 ]})
136
- self .check_external_error_on_write (df )
138
+ self .check_external_error_on_write (df , temp_file )
137
139
138
- def test_rw_use_threads (self ):
140
+ def test_rw_use_threads (self , temp_file ):
139
141
df = pd .DataFrame ({"A" : np .arange (100000 )})
140
- self .check_round_trip (df , use_threads = True )
141
- self .check_round_trip (df , use_threads = False )
142
+ self .check_round_trip (df , temp_file , use_threads = True )
143
+ self .check_round_trip (df , temp_file , use_threads = False )
142
144
143
145
def test_path_pathlib (self ):
144
146
df = pd .DataFrame (
@@ -149,13 +151,13 @@ def test_path_pathlib(self):
149
151
result = tm .round_trip_pathlib (df .to_feather , read_feather )
150
152
tm .assert_frame_equal (df , result )
151
153
152
- def test_passthrough_keywords (self ):
154
+ def test_passthrough_keywords (self , temp_file ):
153
155
df = pd .DataFrame (
154
156
1.1 * np .arange (120 ).reshape ((30 , 4 )),
155
157
columns = pd .Index (list ("ABCD" )),
156
158
index = pd .Index ([f"i-{ i } " for i in range (30 )]),
157
159
).reset_index ()
158
- self .check_round_trip (df , write_kwargs = {"version" : 1 })
160
+ self .check_round_trip (df , temp_file , write_kwargs = {"version" : 1 })
159
161
160
162
@pytest .mark .network
161
163
@pytest .mark .single_cpu
@@ -168,7 +170,7 @@ def test_http_path(self, feather_file, httpserver):
168
170
tm .assert_frame_equal (expected , res )
169
171
170
172
def test_read_feather_dtype_backend (
171
- self , string_storage , dtype_backend , using_infer_string
173
+ self , string_storage , dtype_backend , using_infer_string , temp_file
172
174
):
173
175
# GH#50765
174
176
df = pd .DataFrame (
@@ -184,10 +186,10 @@ def test_read_feather_dtype_backend(
184
186
}
185
187
)
186
188
187
- with tm . ensure_clean () as path :
188
- to_feather (df , path )
189
- with pd .option_context ("mode.string_storage" , string_storage ):
190
- result = read_feather (path , dtype_backend = dtype_backend )
189
+ path = str ( temp_file )
190
+ to_feather (df , path )
191
+ with pd .option_context ("mode.string_storage" , string_storage ):
192
+ result = read_feather (path , dtype_backend = dtype_backend )
191
193
192
194
if dtype_backend == "pyarrow" :
193
195
pa = pytest .importorskip ("pyarrow" )
@@ -227,20 +229,20 @@ def test_read_feather_dtype_backend(
227
229
)
228
230
tm .assert_frame_equal (result , expected )
229
231
230
- def test_int_columns_and_index (self ):
232
+ def test_int_columns_and_index (self , temp_file ):
231
233
df = pd .DataFrame ({"a" : [1 , 2 , 3 ]}, index = pd .Index ([3 , 4 , 5 ], name = "test" ))
232
- self .check_round_trip (df )
234
+ self .check_round_trip (df , temp_file )
233
235
234
- def test_invalid_dtype_backend (self ):
236
+ def test_invalid_dtype_backend (self , temp_file ):
235
237
msg = (
236
238
"dtype_backend numpy is invalid, only 'numpy_nullable' and "
237
239
"'pyarrow' are allowed."
238
240
)
239
241
df = pd .DataFrame ({"int" : list (range (1 , 4 ))})
240
- with tm . ensure_clean ( "tmp.feather" ) as path :
241
- df .to_feather (path )
242
- with pytest .raises (ValueError , match = msg ):
243
- read_feather (path , dtype_backend = "numpy" )
242
+ path = str ( temp_file )
243
+ df .to_feather (path )
244
+ with pytest .raises (ValueError , match = msg ):
245
+ read_feather (path , dtype_backend = "numpy" )
244
246
245
247
def test_string_inference (self , tmp_path , using_infer_string ):
246
248
# GH#54431
@@ -283,7 +285,7 @@ def test_string_inference_string_view_type(self, tmp_path):
283
285
)
284
286
tm .assert_frame_equal (result , expected )
285
287
286
- def test_out_of_bounds_datetime_to_feather (self ):
288
+ def test_out_of_bounds_datetime_to_feather (self , temp_file ):
287
289
# GH#47832
288
290
df = pd .DataFrame (
289
291
{
@@ -293,4 +295,4 @@ def test_out_of_bounds_datetime_to_feather(self):
293
295
],
294
296
}
295
297
)
296
- self .check_round_trip (df )
298
+ self .check_round_trip (df , temp_file )
0 commit comments