diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index b7dcfde327b83..d78403771e227 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1597,7 +1597,6 @@ def test_no_empty_apply(mi_styler): @pytest.mark.parametrize("format", ["html", "latex", "string"]) -def test_output_buffer(mi_styler, format): +def test_output_buffer(mi_styler, format, temp_file): # gh 47053 - with tm.ensure_clean(f"delete_me.{format}") as f: - getattr(mi_styler, f"to_{format}")(f) + getattr(mi_styler, f"to_{format}")(temp_file) diff --git a/pandas/tests/io/formats/test_to_latex.py b/pandas/tests/io/formats/test_to_latex.py index 82f36c94801e3..6cf4771961cbf 100644 --- a/pandas/tests/io/formats/test_to_latex.py +++ b/pandas/tests/io/formats/test_to_latex.py @@ -8,7 +8,6 @@ DataFrame, Series, ) -import pandas._testing as tm pytest.importorskip("jinja2") @@ -30,27 +29,24 @@ def df_short(): class TestToLatex: - def test_to_latex_to_file(self, float_frame): - with tm.ensure_clean("test.tex") as path: - float_frame.to_latex(path) - with open(path, encoding="utf-8") as f: - assert float_frame.to_latex() == f.read() + def test_to_latex_to_file(self, float_frame, temp_file): + float_frame.to_latex(temp_file) + with open(temp_file, encoding="utf-8") as f: + assert float_frame.to_latex() == f.read() - def test_to_latex_to_file_utf8_with_encoding(self): + def test_to_latex_to_file_utf8_with_encoding(self, temp_file): # test with utf-8 and encoding option (GH 7061) df = DataFrame([["au\xdfgangen"]]) - with tm.ensure_clean("test.tex") as path: - df.to_latex(path, encoding="utf-8") - with open(path, encoding="utf-8") as f: - assert df.to_latex() == f.read() + df.to_latex(temp_file, encoding="utf-8") + with open(temp_file, encoding="utf-8") as f: + assert df.to_latex() == f.read() - def test_to_latex_to_file_utf8_without_encoding(self): + def test_to_latex_to_file_utf8_without_encoding(self, temp_file): # test with utf-8 without encoding option df = DataFrame([["au\xdfgangen"]]) - with tm.ensure_clean("test.tex") as path: - df.to_latex(path) - with open(path, encoding="utf-8") as f: - assert df.to_latex() == f.read() + df.to_latex(temp_file) + with open(temp_file, encoding="utf-8") as f: + assert df.to_latex() == f.read() def test_to_latex_tabular_with_index(self): df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) diff --git a/pandas/tests/io/json/test_readlines.py b/pandas/tests/io/json/test_readlines.py index d482eb5fa1a06..f9c25f425a878 100644 --- a/pandas/tests/io/json/test_readlines.py +++ b/pandas/tests/io/json/test_readlines.py @@ -195,7 +195,7 @@ def test_readjson_each_chunk(request, lines_json_df, engine): assert chunks[1].shape == (1, 2) -def test_readjson_chunks_from_file(request, engine): +def test_readjson_chunks_from_file(request, engine, temp_file): if engine == "pyarrow": # GH 48893 reason = ( @@ -204,41 +204,39 @@ def test_readjson_chunks_from_file(request, engine): ) request.applymarker(pytest.mark.xfail(reason=reason, raises=ValueError)) - with tm.ensure_clean("test.json") as path: - df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) - df.to_json(path, lines=True, orient="records") - with read_json(path, lines=True, chunksize=1, engine=engine) as reader: - chunked = pd.concat(reader) - unchunked = read_json(path, lines=True, engine=engine) - tm.assert_frame_equal(unchunked, chunked) + df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) + df.to_json(temp_file, lines=True, orient="records") + with read_json(temp_file, lines=True, chunksize=1, engine=engine) as reader: + chunked = pd.concat(reader) + unchunked = read_json(temp_file, lines=True, engine=engine) + tm.assert_frame_equal(unchunked, chunked) @pytest.mark.parametrize("chunksize", [None, 1]) -def test_readjson_chunks_closes(chunksize): - with tm.ensure_clean("test.json") as path: - df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) - df.to_json(path, lines=True, orient="records") - reader = JsonReader( - path, - orient=None, - typ="frame", - dtype=True, - convert_axes=True, - convert_dates=True, - keep_default_dates=True, - precise_float=False, - date_unit=None, - encoding=None, - lines=True, - chunksize=chunksize, - compression=None, - nrows=None, - ) - with reader: - reader.read() - assert reader.handles.handle.closed, ( - f"didn't close stream with chunksize = {chunksize}" - ) +def test_readjson_chunks_closes(chunksize, temp_file): + df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) + df.to_json(temp_file, lines=True, orient="records") + reader = JsonReader( + temp_file, + orient=None, + typ="frame", + dtype=True, + convert_axes=True, + convert_dates=True, + keep_default_dates=True, + precise_float=False, + date_unit=None, + encoding=None, + lines=True, + chunksize=chunksize, + compression=None, + nrows=None, + ) + with reader: + reader.read() + assert reader.handles.handle.closed, ( + f"didn't close stream with chunksize = {chunksize}" + ) @pytest.mark.parametrize("chunksize", [0, -1, 2.2, "foo"]) @@ -278,7 +276,7 @@ def test_readjson_chunks_multiple_empty_lines(chunksize): tm.assert_frame_equal(orig, test, obj=f"chunksize: {chunksize}") -def test_readjson_unicode(request, monkeypatch, engine): +def test_readjson_unicode(request, monkeypatch, engine, temp_file): if engine == "pyarrow": # GH 48893 reason = ( @@ -287,14 +285,13 @@ def test_readjson_unicode(request, monkeypatch, engine): ) request.applymarker(pytest.mark.xfail(reason=reason, raises=ValueError)) - with tm.ensure_clean("test.json") as path: - monkeypatch.setattr("locale.getpreferredencoding", lambda do_setlocale: "cp949") - with open(path, "w", encoding="utf-8") as f: - f.write('{"£©μÀÆÖÞßéöÿ":["АБВГДабвгд가"]}') + monkeypatch.setattr("locale.getpreferredencoding", lambda do_setlocale: "cp949") + with open(temp_file, "w", encoding="utf-8") as f: + f.write('{"£©μÀÆÖÞßéöÿ":["АБВГДабвгд가"]}') - result = read_json(path, engine=engine) - expected = DataFrame({"£©μÀÆÖÞßéöÿ": ["АБВГДабвгд가"]}) - tm.assert_frame_equal(result, expected) + result = read_json(temp_file, engine=engine) + expected = DataFrame({"£©μÀÆÖÞßéöÿ": ["АБВГДабвгд가"]}) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("nrows", [1, 2]) @@ -441,7 +438,7 @@ def test_to_json_append_mode(mode_): df.to_json(mode=mode_, lines=False, orient="records") -def test_to_json_append_output_consistent_columns(): +def test_to_json_append_output_consistent_columns(temp_file): # GH 35849 # Testing that resulting output reads in as expected. # Testing same columns, new rows @@ -449,17 +446,16 @@ def test_to_json_append_output_consistent_columns(): df2 = DataFrame({"col1": [3, 4], "col2": ["c", "d"]}) expected = DataFrame({"col1": [1, 2, 3, 4], "col2": ["a", "b", "c", "d"]}) - with tm.ensure_clean("test.json") as path: - # Save dataframes to the same file - df1.to_json(path, lines=True, orient="records") - df2.to_json(path, mode="a", lines=True, orient="records") + # Save dataframes to the same file + df1.to_json(temp_file, lines=True, orient="records") + df2.to_json(temp_file, mode="a", lines=True, orient="records") - # Read path file - result = read_json(path, lines=True) - tm.assert_frame_equal(result, expected) + # Read path file + result = read_json(temp_file, lines=True) + tm.assert_frame_equal(result, expected) -def test_to_json_append_output_inconsistent_columns(): +def test_to_json_append_output_inconsistent_columns(temp_file): # GH 35849 # Testing that resulting output reads in as expected. # Testing one new column, one old column, new rows @@ -473,17 +469,16 @@ def test_to_json_append_output_inconsistent_columns(): "col3": [np.nan, np.nan, "!", "#"], } ) - with tm.ensure_clean("test.json") as path: - # Save dataframes to the same file - df1.to_json(path, mode="a", lines=True, orient="records") - df3.to_json(path, mode="a", lines=True, orient="records") + # Save dataframes to the same file + df1.to_json(temp_file, mode="a", lines=True, orient="records") + df3.to_json(temp_file, mode="a", lines=True, orient="records") - # Read path file - result = read_json(path, lines=True) - tm.assert_frame_equal(result, expected) + # Read path file + result = read_json(temp_file, lines=True) + tm.assert_frame_equal(result, expected) -def test_to_json_append_output_different_columns(): +def test_to_json_append_output_different_columns(temp_file): # GH 35849 # Testing that resulting output reads in as expected. # Testing same, differing and new columns @@ -500,19 +495,18 @@ def test_to_json_append_output_different_columns(): "col4": [None, None, None, None, None, None, True, False], } ).astype({"col4": "float"}) - with tm.ensure_clean("test.json") as path: - # Save dataframes to the same file - df1.to_json(path, mode="a", lines=True, orient="records") - df2.to_json(path, mode="a", lines=True, orient="records") - df3.to_json(path, mode="a", lines=True, orient="records") - df4.to_json(path, mode="a", lines=True, orient="records") - - # Read path file - result = read_json(path, lines=True) - tm.assert_frame_equal(result, expected) + # Save dataframes to the same file + df1.to_json(temp_file, mode="a", lines=True, orient="records") + df2.to_json(temp_file, mode="a", lines=True, orient="records") + df3.to_json(temp_file, mode="a", lines=True, orient="records") + df4.to_json(temp_file, mode="a", lines=True, orient="records") + + # Read path file + result = read_json(temp_file, lines=True) + tm.assert_frame_equal(result, expected) -def test_to_json_append_output_different_columns_reordered(): +def test_to_json_append_output_different_columns_reordered(temp_file): # GH 35849 # Testing that resulting output reads in as expected. # Testing specific result column order. @@ -530,13 +524,12 @@ def test_to_json_append_output_different_columns_reordered(): "col1": [None, None, None, None, 3, 4, 1, 2], } ).astype({"col4": "float"}) - with tm.ensure_clean("test.json") as path: - # Save dataframes to the same file - df4.to_json(path, mode="a", lines=True, orient="records") - df3.to_json(path, mode="a", lines=True, orient="records") - df2.to_json(path, mode="a", lines=True, orient="records") - df1.to_json(path, mode="a", lines=True, orient="records") - - # Read path file - result = read_json(path, lines=True) - tm.assert_frame_equal(result, expected) + # Save dataframes to the same file + df4.to_json(temp_file, mode="a", lines=True, orient="records") + df3.to_json(temp_file, mode="a", lines=True, orient="records") + df2.to_json(temp_file, mode="a", lines=True, orient="records") + df1.to_json(temp_file, mode="a", lines=True, orient="records") + + # Read path file + result = read_json(temp_file, lines=True) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/io/test_feather.py b/pandas/tests/io/test_feather.py index 904c3a047bab2..14643587465ea 100644 --- a/pandas/tests/io/test_feather.py +++ b/pandas/tests/io/test_feather.py @@ -26,36 +26,35 @@ @pytest.mark.single_cpu class TestFeather: - def check_error_on_write(self, df, exc, err_msg): + def check_error_on_write(self, df, exc, err_msg, temp_file): # check that we are raising the exception # on writing with pytest.raises(exc, match=err_msg): - with tm.ensure_clean() as path: - to_feather(df, path) + to_feather(df, temp_file) - def check_external_error_on_write(self, df): + def check_external_error_on_write(self, df, temp_file): # check that we are raising the exception # on writing with tm.external_error_raised(Exception): - with tm.ensure_clean() as path: - to_feather(df, path) + to_feather(df, temp_file) - def check_round_trip(self, df, expected=None, write_kwargs=None, **read_kwargs): + def check_round_trip( + self, df, temp_file, expected=None, write_kwargs=None, **read_kwargs + ): if write_kwargs is None: write_kwargs = {} if expected is None: expected = df.copy() - with tm.ensure_clean() as path: - to_feather(df, path, **write_kwargs) + to_feather(df, temp_file, **write_kwargs) - result = read_feather(path, **read_kwargs) + result = read_feather(temp_file, **read_kwargs) - tm.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) - def test_error(self): + def test_error(self, temp_file): msg = "feather only support IO with DataFrames" for obj in [ pd.Series([1, 2, 3]), @@ -64,9 +63,9 @@ def test_error(self): pd.Timestamp("20130101"), np.array([1, 2, 3]), ]: - self.check_error_on_write(obj, ValueError, msg) + self.check_error_on_write(obj, ValueError, msg, temp_file) - def test_basic(self): + def test_basic(self, temp_file): tz = zoneinfo.ZoneInfo("US/Eastern") df = pd.DataFrame( { @@ -103,15 +102,15 @@ def test_basic(self): expected = df.copy() expected.loc[1, "bool_with_null"] = None - self.check_round_trip(df, expected=expected) + self.check_round_trip(df, temp_file, expected=expected) - def test_duplicate_columns(self): + def test_duplicate_columns(self, temp_file): # https://github.com/wesm/feather/issues/53 # not currently able to handle duplicate columns df = pd.DataFrame(np.arange(12).reshape(4, 3), columns=list("aaa")).copy() - self.check_external_error_on_write(df) + self.check_external_error_on_write(df, temp_file) - def test_read_columns(self): + def test_read_columns(self, temp_file): # GH 24025 df = pd.DataFrame( { @@ -122,23 +121,23 @@ def test_read_columns(self): } ) columns = ["col1", "col3"] - self.check_round_trip(df, expected=df[columns], columns=columns) + self.check_round_trip(df, temp_file, expected=df[columns], columns=columns) - def test_read_columns_different_order(self): + def test_read_columns_different_order(self, temp_file): # GH 33878 df = pd.DataFrame({"A": [1, 2], "B": ["x", "y"], "C": [True, False]}) expected = df[["B", "A"]] - self.check_round_trip(df, expected, columns=["B", "A"]) + self.check_round_trip(df, temp_file, expected, columns=["B", "A"]) - def test_unsupported_other(self): + def test_unsupported_other(self, temp_file): # mixed python objects df = pd.DataFrame({"a": ["a", 1, 2.0]}) - self.check_external_error_on_write(df) + self.check_external_error_on_write(df, temp_file) - def test_rw_use_threads(self): + def test_rw_use_threads(self, temp_file): df = pd.DataFrame({"A": np.arange(100000)}) - self.check_round_trip(df, use_threads=True) - self.check_round_trip(df, use_threads=False) + self.check_round_trip(df, temp_file, use_threads=True) + self.check_round_trip(df, temp_file, use_threads=False) def test_path_pathlib(self): df = pd.DataFrame( @@ -149,13 +148,13 @@ def test_path_pathlib(self): result = tm.round_trip_pathlib(df.to_feather, read_feather) tm.assert_frame_equal(df, result) - def test_passthrough_keywords(self): + def test_passthrough_keywords(self, temp_file): df = pd.DataFrame( 1.1 * np.arange(120).reshape((30, 4)), columns=pd.Index(list("ABCD")), index=pd.Index([f"i-{i}" for i in range(30)]), ).reset_index() - self.check_round_trip(df, write_kwargs={"version": 1}) + self.check_round_trip(df, temp_file, write_kwargs={"version": 1}) @pytest.mark.network @pytest.mark.single_cpu @@ -168,7 +167,7 @@ def test_http_path(self, feather_file, httpserver): tm.assert_frame_equal(expected, res) def test_read_feather_dtype_backend( - self, string_storage, dtype_backend, using_infer_string + self, string_storage, dtype_backend, using_infer_string, temp_file ): # GH#50765 df = pd.DataFrame( @@ -184,10 +183,9 @@ def test_read_feather_dtype_backend( } ) - with tm.ensure_clean() as path: - to_feather(df, path) - with pd.option_context("mode.string_storage", string_storage): - result = read_feather(path, dtype_backend=dtype_backend) + to_feather(df, temp_file) + with pd.option_context("mode.string_storage", string_storage): + result = read_feather(temp_file, dtype_backend=dtype_backend) if dtype_backend == "pyarrow": pa = pytest.importorskip("pyarrow") @@ -227,20 +225,19 @@ def test_read_feather_dtype_backend( ) tm.assert_frame_equal(result, expected) - def test_int_columns_and_index(self): + def test_int_columns_and_index(self, temp_file): df = pd.DataFrame({"a": [1, 2, 3]}, index=pd.Index([3, 4, 5], name="test")) - self.check_round_trip(df) + self.check_round_trip(df, temp_file) - def test_invalid_dtype_backend(self): + def test_invalid_dtype_backend(self, temp_file): msg = ( "dtype_backend numpy is invalid, only 'numpy_nullable' and " "'pyarrow' are allowed." ) df = pd.DataFrame({"int": list(range(1, 4))}) - with tm.ensure_clean("tmp.feather") as path: - df.to_feather(path) - with pytest.raises(ValueError, match=msg): - read_feather(path, dtype_backend="numpy") + df.to_feather(temp_file) + with pytest.raises(ValueError, match=msg): + read_feather(temp_file, dtype_backend="numpy") def test_string_inference(self, tmp_path, using_infer_string): # GH#54431 @@ -283,7 +280,7 @@ def test_string_inference_string_view_type(self, tmp_path): ) tm.assert_frame_equal(result, expected) - def test_out_of_bounds_datetime_to_feather(self): + def test_out_of_bounds_datetime_to_feather(self, temp_file): # GH#47832 df = pd.DataFrame( { @@ -293,4 +290,4 @@ def test_out_of_bounds_datetime_to_feather(self): ], } ) - self.check_round_trip(df) + self.check_round_trip(df, temp_file) diff --git a/pandas/tests/io/test_orc.py b/pandas/tests/io/test_orc.py index 2c193c968e2b5..1fb285914e8d2 100644 --- a/pandas/tests/io/test_orc.py +++ b/pandas/tests/io/test_orc.py @@ -4,7 +4,6 @@ from decimal import Decimal from io import BytesIO import os -import pathlib import numpy as np import pytest @@ -231,7 +230,7 @@ def test_orc_reader_snappy_compressed(dirpath): tm.assert_equal(expected, got) -def test_orc_roundtrip_file(dirpath): +def test_orc_roundtrip_file(dirpath, temp_file): # GH44554 # PyArrow gained ORC write support with the current argument order pytest.importorskip("pyarrow") @@ -249,11 +248,10 @@ def test_orc_roundtrip_file(dirpath): } expected = pd.DataFrame.from_dict(data) - with tm.ensure_clean() as path: - expected.to_orc(path) - got = read_orc(path) + expected.to_orc(temp_file) + got = read_orc(temp_file) - tm.assert_equal(expected, got) + tm.assert_equal(expected, got) def test_orc_roundtrip_bytesio(): @@ -383,12 +381,11 @@ def test_orc_dtype_backend_numpy_nullable(): tm.assert_frame_equal(result, expected) -def test_orc_uri_path(): +def test_orc_uri_path(temp_file): expected = pd.DataFrame({"int": list(range(1, 4))}) - with tm.ensure_clean("tmp.orc") as path: - expected.to_orc(path) - uri = pathlib.Path(path).as_uri() - result = read_orc(uri) + expected.to_orc(temp_file) + uri = temp_file.as_uri() + result = read_orc(uri) tm.assert_frame_equal(result, expected) @@ -410,16 +407,15 @@ def test_to_orc_non_default_index(index): df.to_orc() -def test_invalid_dtype_backend(): +def test_invalid_dtype_backend(temp_file): msg = ( "dtype_backend numpy is invalid, only 'numpy_nullable' and " "'pyarrow' are allowed." ) df = pd.DataFrame({"int": list(range(1, 4))}) - with tm.ensure_clean("tmp.orc") as path: - df.to_orc(path) - with pytest.raises(ValueError, match=msg): - read_orc(path, dtype_backend="numpy") + df.to_orc(temp_file) + with pytest.raises(ValueError, match=msg): + read_orc(temp_file, dtype_backend="numpy") def test_string_inference(tmp_path):

AltStyle によって変換されたページ (->オリジナル) /