Skip to content

Commit 204369b

Browse files
committed
test_feather updated
1 parent 53cb639 commit 204369b

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

pandas/tests/io/test_feather.py

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,38 @@
2626

2727
@pytest.mark.single_cpu
2828
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):
3030
# check that we are raising the exception
3131
# on writing
3232

3333
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)
3636

37-
def check_external_error_on_write(self, df):
37+
def check_external_error_on_write(self, df, temp_file):
3838
# check that we are raising the exception
3939
# on writing
4040

4141
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)
4444

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+
):
4648
if write_kwargs is None:
4749
write_kwargs = {}
4850
if expected is None:
4951
expected = df.copy()
5052

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)
5355

54-
result = read_feather(path, **read_kwargs)
56+
result = read_feather(path, **read_kwargs)
5557

56-
tm.assert_frame_equal(result, expected)
58+
tm.assert_frame_equal(result, expected)
5759

58-
def test_error(self):
60+
def test_error(self, temp_file):
5961
msg = "feather only support IO with DataFrames"
6062
for obj in [
6163
pd.Series([1, 2, 3]),
@@ -64,9 +66,9 @@ def test_error(self):
6466
pd.Timestamp("20130101"),
6567
np.array([1, 2, 3]),
6668
]:
67-
self.check_error_on_write(obj, ValueError, msg)
69+
self.check_error_on_write(obj, ValueError, msg, temp_file)
6870

69-
def test_basic(self):
71+
def test_basic(self, temp_file):
7072
tz = zoneinfo.ZoneInfo("US/Eastern")
7173
df = pd.DataFrame(
7274
{
@@ -103,15 +105,15 @@ def test_basic(self):
103105

104106
expected = df.copy()
105107
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)
107109

108-
def test_duplicate_columns(self):
110+
def test_duplicate_columns(self, temp_file):
109111
# https://github.com/wesm/feather/issues/53
110112
# not currently able to handle duplicate columns
111113
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)
113115

114-
def test_read_columns(self):
116+
def test_read_columns(self, temp_file):
115117
# GH 24025
116118
df = pd.DataFrame(
117119
{
@@ -122,23 +124,23 @@ def test_read_columns(self):
122124
}
123125
)
124126
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)
126128

127-
def test_read_columns_different_order(self):
129+
def test_read_columns_different_order(self, temp_file):
128130
# GH 33878
129131
df = pd.DataFrame({"A": [1, 2], "B": ["x", "y"], "C": [True, False]})
130132
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"])
132134

133-
def test_unsupported_other(self):
135+
def test_unsupported_other(self, temp_file):
134136
# mixed python objects
135137
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)
137139

138-
def test_rw_use_threads(self):
140+
def test_rw_use_threads(self, temp_file):
139141
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)
142144

143145
def test_path_pathlib(self):
144146
df = pd.DataFrame(
@@ -149,13 +151,13 @@ def test_path_pathlib(self):
149151
result = tm.round_trip_pathlib(df.to_feather, read_feather)
150152
tm.assert_frame_equal(df, result)
151153

152-
def test_passthrough_keywords(self):
154+
def test_passthrough_keywords(self, temp_file):
153155
df = pd.DataFrame(
154156
1.1 * np.arange(120).reshape((30, 4)),
155157
columns=pd.Index(list("ABCD")),
156158
index=pd.Index([f"i-{i}" for i in range(30)]),
157159
).reset_index()
158-
self.check_round_trip(df, write_kwargs={"version": 1})
160+
self.check_round_trip(df, temp_file, write_kwargs={"version": 1})
159161

160162
@pytest.mark.network
161163
@pytest.mark.single_cpu
@@ -168,7 +170,7 @@ def test_http_path(self, feather_file, httpserver):
168170
tm.assert_frame_equal(expected, res)
169171

170172
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
172174
):
173175
# GH#50765
174176
df = pd.DataFrame(
@@ -184,10 +186,10 @@ def test_read_feather_dtype_backend(
184186
}
185187
)
186188

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)
191193

192194
if dtype_backend == "pyarrow":
193195
pa = pytest.importorskip("pyarrow")
@@ -227,20 +229,20 @@ def test_read_feather_dtype_backend(
227229
)
228230
tm.assert_frame_equal(result, expected)
229231

230-
def test_int_columns_and_index(self):
232+
def test_int_columns_and_index(self, temp_file):
231233
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)
233235

234-
def test_invalid_dtype_backend(self):
236+
def test_invalid_dtype_backend(self, temp_file):
235237
msg = (
236238
"dtype_backend numpy is invalid, only 'numpy_nullable' and "
237239
"'pyarrow' are allowed."
238240
)
239241
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")
244246

245247
def test_string_inference(self, tmp_path, using_infer_string):
246248
# GH#54431
@@ -283,7 +285,7 @@ def test_string_inference_string_view_type(self, tmp_path):
283285
)
284286
tm.assert_frame_equal(result, expected)
285287

286-
def test_out_of_bounds_datetime_to_feather(self):
288+
def test_out_of_bounds_datetime_to_feather(self, temp_file):
287289
# GH#47832
288290
df = pd.DataFrame(
289291
{
@@ -293,4 +295,4 @@ def test_out_of_bounds_datetime_to_feather(self):
293295
],
294296
}
295297
)
296-
self.check_round_trip(df)
298+
self.check_round_trip(df, temp_file)

0 commit comments

Comments
 (0)