Skip to content
28 changes: 7 additions & 21 deletions pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
TYPE_CHECKING,
Any,
)
import uuid
import zipfile

from pandas.compat._optional import import_optional_dependency
Expand All @@ -16,11 +15,7 @@

if TYPE_CHECKING:
from collections.abc import Callable

from pandas._typing import (
FilePath,
ReadPickleBuffer,
)
from pathlib import Path

from pandas import (
DataFrame,
Expand All @@ -31,9 +26,7 @@
# File-IO


def round_trip_pickle(
obj: Any, tmp_path, path: FilePath | ReadPickleBuffer | None = None
) -> DataFrame | Series:
def round_trip_pickle(obj: Any, tmp_path: Path) -> DataFrame | Series:
"""
Pickle an object and then read it again.

Expand All @@ -49,15 +42,11 @@ def round_trip_pickle(
pandas object
The original object that was pickled and then re-read.
"""
_path = path
if _path is None:
_path = f"__{uuid.uuid4()}__.pickle"
temp_path = tmp_path / _path
pd.to_pickle(obj, temp_path)
return pd.read_pickle(temp_path)
pd.to_pickle(obj, tmp_path)
return pd.read_pickle(tmp_path)


def round_trip_pathlib(writer, reader, tmp_path, path: str | None = None):
def round_trip_pathlib(writer, reader, tmp_path: Path):
"""
Write an object to file specified by a pathlib.Path and read it back

Expand All @@ -75,11 +64,8 @@ def round_trip_pathlib(writer, reader, tmp_path, path: str | None = None):
pandas object
The original object that was serialized and then re-read.
"""
if path is None:
path = "___pathlib___"
path = tmp_path / path
writer(path)
obj = reader(path)
writer(tmp_path)
obj = reader(tmp_path)
return obj


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ def test_dense_repr(self, vals, fill_value):
tm.assert_numpy_array_equal(res, vals)

@pytest.mark.parametrize("fix", ["arr", "zarr"])
def test_pickle(self, fix, request, tmp_path):
def test_pickle(self, fix, request, temp_file):
obj = request.getfixturevalue(fix)
unpickled = tm.round_trip_pickle(obj, tmp_path)
unpickled = tm.round_trip_pickle(obj, temp_file)
tm.assert_sp_array_equal(unpickled, obj)

def test_generator_warnings(self):
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def test_numpy_informed(self, dtype):
assert not dtype == np.str_
assert not np.str_ == dtype

def test_pickle(self, dtype, tmp_path):
def test_pickle(self, dtype, temp_file):
# make sure our cache is NOT pickled

# clear the cache
type(dtype).reset_cache()
assert not len(dtype._cache_dtypes)

# force back to the cache
result = tm.round_trip_pickle(dtype, tmp_path)
result = tm.round_trip_pickle(dtype, temp_file)
if not isinstance(dtype, PeriodDtype):
# Because PeriodDtype has a cython class as a base class,
# it has different pickle semantics, and its cache is re-populated
Expand Down Expand Up @@ -848,7 +848,7 @@ def test_basic_dtype(self):
assert not is_interval_dtype(np.int64)
assert not is_interval_dtype(np.float64)

def test_caching(self, tmp_path):
def test_caching(self, temp_file):
# GH 54184: Caching not shown to improve performance
IntervalDtype.reset_cache()
dtype = IntervalDtype("int64", "right")
Expand All @@ -858,20 +858,20 @@ def test_caching(self, tmp_path):
assert len(IntervalDtype._cache_dtypes) == 0

IntervalDtype.reset_cache()
tm.round_trip_pickle(dtype, tmp_path)
tm.round_trip_pickle(dtype, temp_file)
assert len(IntervalDtype._cache_dtypes) == 0

def test_not_string(self):
# GH30568: though IntervalDtype has object kind, it cannot be string
assert not is_string_dtype(IntervalDtype())

def test_unpickling_without_closed(self, tmp_path):
def test_unpickling_without_closed(self, temp_file):
# GH#38394
dtype = IntervalDtype("interval")

assert dtype._closed is None

tm.round_trip_pickle(dtype, tmp_path)
tm.round_trip_pickle(dtype, temp_file)

def test_dont_keep_ref_after_del(self):
# GH 54184
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,20 @@ def f(dtype):
with pytest.raises(ValueError, match=msg):
f("M8[ns]")

def test_pickle_float_string_frame(self, float_string_frame, tmp_path):
unpickled = tm.round_trip_pickle(float_string_frame, tmp_path)
def test_pickle_float_string_frame(self, float_string_frame, temp_file):
unpickled = tm.round_trip_pickle(float_string_frame, temp_file)
tm.assert_frame_equal(float_string_frame, unpickled)

# buglet
float_string_frame._mgr.ndim

def test_pickle_empty(self, tmp_path):
def test_pickle_empty(self, temp_file):
empty_frame = DataFrame()
unpickled = tm.round_trip_pickle(empty_frame, tmp_path)
unpickled = tm.round_trip_pickle(empty_frame, temp_file)
repr(unpickled)

def test_pickle_empty_tz_frame(self, timezone_frame, tmp_path):
unpickled = tm.round_trip_pickle(timezone_frame, tmp_path)
def test_pickle_empty_tz_frame(self, timezone_frame, temp_file):
unpickled = tm.round_trip_pickle(timezone_frame, temp_file)
tm.assert_frame_equal(timezone_frame, unpickled)

def test_consolidate_datetime64(self):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def custom_frame_function(self):
cdf_multi2 = CustomDataFrame([[0, 1], [2, 3]], columns=mcol)
assert isinstance(cdf_multi2["A"], CustomSeries)

def test_dataframe_metadata(self, tmp_path):
def test_dataframe_metadata(self, temp_file):
df = tm.SubclassedDataFrame(
{"X": [1, 2, 3], "Y": [1, 2, 3]}, index=["a", "b", "c"]
)
Expand All @@ -97,7 +97,7 @@ def test_dataframe_metadata(self, tmp_path):
assert df.iloc[0:1, :].testattr == "XXX"

# see gh-10553
unpickled = tm.round_trip_pickle(df, tmp_path)
unpickled = tm.round_trip_pickle(df, temp_file)
tm.assert_frame_equal(df, unpickled)
assert df._metadata == unpickled._metadata
assert df.testattr == unpickled.testattr
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/generic/test_duplicate_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,11 @@ def test_inplace_raises(method, frame_only):
method(s)


def test_pickle(tmp_path):
def test_pickle(temp_file):
a = pd.Series([1, 2]).set_flags(allows_duplicate_labels=False)
b = tm.round_trip_pickle(a, tmp_path)
b = tm.round_trip_pickle(a, temp_file)
tm.assert_series_equal(a, b)

a = pd.DataFrame({"A": []}).set_flags(allows_duplicate_labels=False)
b = tm.round_trip_pickle(a, tmp_path)
b = tm.round_trip_pickle(a, temp_file)
tm.assert_frame_equal(a, b)
4 changes: 2 additions & 2 deletions pandas/tests/indexes/base_class/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import pandas._testing as tm


def test_pickle_preserves_object_dtype(tmp_path):
def test_pickle_preserves_object_dtype(temp_file):
# GH#43188, GH#43155 don't infer numeric dtype
index = Index([1, 2, 3], dtype=object)

result = tm.round_trip_pickle(index, tmp_path)
result = tm.round_trip_pickle(index, temp_file)
assert result.dtype == object
tm.assert_index_equal(index, result)
20 changes: 10 additions & 10 deletions pandas/tests/indexes/datetimes/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@


class TestPickle:
def test_pickle(self, tmp_path):
def test_pickle(self, temp_file):
# GH#4606
idx = to_datetime(["2013-01-01", NaT, "2014-01-06"])
idx_p = tm.round_trip_pickle(idx, tmp_path)
idx_p = tm.round_trip_pickle(idx, temp_file)
assert idx_p[0] == idx[0]
assert idx_p[1] is NaT
assert idx_p[2] == idx[2]

def test_pickle_dont_infer_freq(self, tmp_path):
def test_pickle_dont_infer_freq(self, temp_file):
# GH#11002
# don't infer freq
idx = date_range("1750-1-1", "2050-1-1", freq="7D")
idx_p = tm.round_trip_pickle(idx, tmp_path)
idx_p = tm.round_trip_pickle(idx, temp_file)
tm.assert_index_equal(idx, idx_p)

def test_pickle_after_set_freq(self, tmp_path):
def test_pickle_after_set_freq(self, temp_file):
dti = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
dti = dti._with_freq(None)

res = tm.round_trip_pickle(dti, tmp_path)
res = tm.round_trip_pickle(dti, temp_file)
tm.assert_index_equal(res, dti)

def test_roundtrip_pickle_with_tz(self, tmp_path):
def test_roundtrip_pickle_with_tz(self, temp_file):
# GH#8367
# round-trip of timezone
index = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
unpickled = tm.round_trip_pickle(index, tmp_path)
unpickled = tm.round_trip_pickle(index, temp_file)
tm.assert_index_equal(index, unpickled)

@pytest.mark.parametrize("freq", ["B", "C"])
def test_pickle_unpickle(self, freq, tmp_path):
def test_pickle_unpickle(self, freq, temp_file):
rng = date_range("2009-01-01", "2010-01-01", freq=freq)
unpickled = tm.round_trip_pickle(rng, tmp_path)
unpickled = tm.round_trip_pickle(rng, temp_file)
assert unpickled.freq == freq
4 changes: 2 additions & 2 deletions pandas/tests/indexes/interval/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


class TestPickle:
def test_pickle_round_trip_closed(self, closed, tmp_path):
def test_pickle_round_trip_closed(self, closed, temp_file):
# https://github.com/pandas-dev/pandas/issues/35658
idx = IntervalIndex.from_tuples([(1, 2), (2, 3)], closed=closed)
result = tm.round_trip_pickle(idx, tmp_path)
result = tm.round_trip_pickle(idx, temp_file)
tm.assert_index_equal(result, idx)
8 changes: 4 additions & 4 deletions pandas/tests/indexes/period/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

class TestPickle:
@pytest.mark.parametrize("freq", ["D", "M", "Y"])
def test_pickle_round_trip(self, freq, tmp_path):
def test_pickle_round_trip(self, freq, temp_file):
idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.nan], freq=freq)
result = tm.round_trip_pickle(idx, tmp_path)
result = tm.round_trip_pickle(idx, temp_file)
tm.assert_index_equal(result, idx)

def test_pickle_freq(self, tmp_path):
def test_pickle_freq(self, temp_file):
# GH#2891
prng = period_range("1/1/2011", "1/1/2012", freq="M")
new_prng = tm.round_trip_pickle(prng, tmp_path)
new_prng = tm.round_trip_pickle(prng, temp_file)
assert new_prng.freq == offsets.MonthEnd()
assert new_prng.freqstr == "M"
8 changes: 4 additions & 4 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ def test_tolist_matches_list(self, index):


class TestRoundTrips:
def test_pickle_roundtrip(self, index, tmp_path):
result = tm.round_trip_pickle(index, tmp_path)
def test_pickle_roundtrip(self, index, temp_file):
result = tm.round_trip_pickle(index, temp_file)
tm.assert_index_equal(result, index, exact=True)
if result.nlevels > 1:
# GH#8367 round-trip with timezone
assert index.equal_levels(result)

def test_pickle_preserves_name(self, index, tmp_path):
def test_pickle_preserves_name(self, index, temp_file):
original_name, index.name = index.name, "foo"
unpickled = tm.round_trip_pickle(index, tmp_path)
unpickled = tm.round_trip_pickle(index, temp_file)
assert index.equals(unpickled)
index.name = original_name

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/timedeltas/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


class TestPickle:
def test_pickle_after_set_freq(self, tmp_path):
def test_pickle_after_set_freq(self, temp_file):
tdi = timedelta_range("1 day", periods=4, freq="s")
tdi = tdi._with_freq(None)

res = tm.round_trip_pickle(tdi, tmp_path)
res = tm.round_trip_pickle(tdi, temp_file)
tm.assert_index_equal(res, tdi)
18 changes: 9 additions & 9 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ def test_constructor(self):
["bool", [5]],
],
)
def test_pickle(self, typ, data, tmp_path):
def test_pickle(self, typ, data, temp_file):
blk = create_block(typ, data)
assert_block_equal(tm.round_trip_pickle(blk, tmp_path), blk)
assert_block_equal(tm.round_trip_pickle(blk, temp_file), blk)

def test_mgr_locs(self, fblock):
assert isinstance(fblock.mgr_locs, BlockPlacement)
Expand Down Expand Up @@ -391,8 +391,8 @@ def test_duplicate_ref_loc_failure(self):
mgr = BlockManager(blocks, axes)
mgr.iget(1)

def test_pickle(self, mgr, tmp_path):
mgr2 = tm.round_trip_pickle(mgr, tmp_path)
def test_pickle(self, mgr, temp_file):
mgr2 = tm.round_trip_pickle(mgr, temp_file)
tm.assert_frame_equal(
DataFrame._from_mgr(mgr, axes=mgr.axes),
DataFrame._from_mgr(mgr2, axes=mgr2.axes),
Expand All @@ -407,24 +407,24 @@ def test_pickle(self, mgr, tmp_path):
assert not mgr2._known_consolidated

@pytest.mark.parametrize("mgr_string", ["a,a,a:f8", "a: f8; a: i8"])
def test_non_unique_pickle(self, mgr_string, tmp_path):
def test_non_unique_pickle(self, mgr_string, temp_file):
mgr = create_mgr(mgr_string)
mgr2 = tm.round_trip_pickle(mgr, tmp_path)
mgr2 = tm.round_trip_pickle(mgr, temp_file)
tm.assert_frame_equal(
DataFrame._from_mgr(mgr, axes=mgr.axes),
DataFrame._from_mgr(mgr2, axes=mgr2.axes),
)

def test_categorical_block_pickle(self, tmp_path):
def test_categorical_block_pickle(self, temp_file):
mgr = create_mgr("a: category")
mgr2 = tm.round_trip_pickle(mgr, tmp_path)
mgr2 = tm.round_trip_pickle(mgr, temp_file)
tm.assert_frame_equal(
DataFrame._from_mgr(mgr, axes=mgr.axes),
DataFrame._from_mgr(mgr2, axes=mgr2.axes),
)

smgr = create_single_mgr("category")
smgr2 = tm.round_trip_pickle(smgr, tmp_path)
smgr2 = tm.round_trip_pickle(smgr, temp_file)
tm.assert_series_equal(
Series()._constructor_from_mgr(smgr, axes=smgr.axes),
Series()._constructor_from_mgr(smgr2, axes=smgr2.axes),
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from functools import partial
from io import BytesIO
import os
import pathlib
import re
import uuid

Expand Down Expand Up @@ -1422,7 +1423,7 @@ def test_freeze_panes(self, tmp_excel):
result = pd.read_excel(tmp_excel, index_col=0)
tm.assert_frame_equal(result, expected)

def test_path_path_lib(self, engine, ext, tmp_path):
def test_path_path_lib(self, engine, tmp_excel):
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD")),
Expand All @@ -1431,7 +1432,7 @@ def test_path_path_lib(self, engine, ext, tmp_path):
writer = partial(df.to_excel, engine=engine)

reader = partial(pd.read_excel, index_col=0)
result = tm.round_trip_pathlib(writer, reader, tmp_path, path=f"foo{ext}")
result = tm.round_trip_pathlib(writer, reader, pathlib.Path(tmp_excel))
tm.assert_frame_equal(result, df)

def test_merged_cell_custom_objects(self, tmp_excel):
Expand Down
Loading
Loading