Skip to content

TST/CLN: Remove ensure_clean_path for tmp_dir fixture #48925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 6 additions & 34 deletions pandas/tests/io/pytables/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
tables.parameters.MAX_THREADS = 1


def safe_remove(path):
if path is not None:
try:
os.remove(path) # noqa: PDF008
except OSError:
pass


def safe_close(store):
try:
if store is not None:
Expand All @@ -30,11 +22,6 @@ def safe_close(store):
pass


def create_tempfile(path):
"""create an unopened named temporary file"""
return os.path.join(tempfile.gettempdir(), path)


# contextmanager to ensure the file cleanup
@contextmanager
def ensure_clean_store(
Expand All @@ -45,7 +32,7 @@ def ensure_clean_store(

# put in the temporary path if we don't have one already
if not len(os.path.dirname(path)):
path = create_tempfile(path)
path = os.path.join(tempfile.gettempdir(), path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking: Is there a reason for using os instead of pathlib? Would prefer the latter because of platform independency

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not; this was just copying over code from create_tempfile. Willl use pathlib here


store = HDFStore(
path, mode=mode, complevel=complevel, complib=complib, fletcher32=False
Expand All @@ -54,26 +41,11 @@ def ensure_clean_store(
finally:
safe_close(store)
if mode == "w" or mode == "a":
safe_remove(path)


@contextmanager
def ensure_clean_path(path):
"""
return essentially a named temporary file that is not opened
and deleted on exiting; if path is a list, then create and
return list of filenames
"""
try:
if isinstance(path, list):
filenames = [create_tempfile(p) for p in path]
yield filenames
else:
filenames = [create_tempfile(path)]
yield filenames[0]
finally:
for f in filenames:
safe_remove(f)
if path is not None:
try:
os.remove(path) # noqa: PDF008
except OSError:
pass


def _maybe_remove(store, key):
Expand Down
13 changes: 6 additions & 7 deletions pandas/tests/io/pytables/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
)
from pandas.tests.io.pytables.common import (
_maybe_remove,
ensure_clean_path,
ensure_clean_store,
)

Expand Down Expand Up @@ -634,7 +633,7 @@ def check_col(key, name, size):
tm.assert_frame_equal(result, expected)


def test_append_hierarchical(setup_path, multiindex_dataframe_random_data):
def test_append_hierarchical(tmp_path, setup_path, multiindex_dataframe_random_data):
df = multiindex_dataframe_random_data
df.columns.name = None

Expand All @@ -648,11 +647,11 @@ def test_append_hierarchical(setup_path, multiindex_dataframe_random_data):
expected = df.reindex(columns=["A", "B"])
tm.assert_frame_equal(result, expected)

with ensure_clean_path("test.hdf") as path:
df.to_hdf(path, "df", format="table")
result = read_hdf(path, "df", columns=["A", "B"])
expected = df.reindex(columns=["A", "B"])
tm.assert_frame_equal(result, expected)
path = tmp_path / "test.hdf"
df.to_hdf(path, "df", format="table")
result = read_hdf(path, "df", columns=["A", "B"])
expected = df.reindex(columns=["A", "B"])
tm.assert_frame_equal(result, expected)


def test_append_misc(setup_path):
Expand Down
41 changes: 21 additions & 20 deletions pandas/tests/io/pytables/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)
from pandas.tests.io.pytables.common import (
_maybe_remove,
ensure_clean_path,
ensure_clean_store,
)

Expand Down Expand Up @@ -147,7 +146,7 @@ def test_categorical(setup_path):
store.select("df3/meta/s/meta")


def test_categorical_conversion(setup_path):
def test_categorical_conversion(tmp_path, setup_path):

# GH13322
# Check that read_hdf with categorical columns doesn't return rows if
Expand All @@ -161,24 +160,24 @@ def test_categorical_conversion(setup_path):

# We are expecting an empty DataFrame matching types of df
expected = df.iloc[[], :]
with ensure_clean_path(setup_path) as path:
df.to_hdf(path, "df", format="table", data_columns=True)
result = read_hdf(path, "df", where="obsids=B")
tm.assert_frame_equal(result, expected)
path = tmp_path / setup_path
df.to_hdf(path, "df", format="table", data_columns=True)
result = read_hdf(path, "df", where="obsids=B")
tm.assert_frame_equal(result, expected)

# Test with categories
df.obsids = df.obsids.astype("category")
df.imgids = df.imgids.astype("category")

# We are expecting an empty DataFrame matching types of df
expected = df.iloc[[], :]
with ensure_clean_path(setup_path) as path:
df.to_hdf(path, "df", format="table", data_columns=True)
result = read_hdf(path, "df", where="obsids=B")
tm.assert_frame_equal(result, expected)
path = tmp_path / setup_path
df.to_hdf(path, "df", format="table", data_columns=True)
result = read_hdf(path, "df", where="obsids=B")
tm.assert_frame_equal(result, expected)


def test_categorical_nan_only_columns(setup_path):
def test_categorical_nan_only_columns(tmp_path, setup_path):
# GH18413
# Check that read_hdf with categorical columns with NaN-only values can
# be read back.
Expand All @@ -194,10 +193,10 @@ def test_categorical_nan_only_columns(setup_path):
df["b"] = df.b.astype("category")
df["d"] = df.b.astype("category")
expected = df
with ensure_clean_path(setup_path) as path:
df.to_hdf(path, "df", format="table", data_columns=True)
result = read_hdf(path, "df")
tm.assert_frame_equal(result, expected)
path = tmp_path / setup_path
df.to_hdf(path, "df", format="table", data_columns=True)
result = read_hdf(path, "df")
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
Expand All @@ -207,7 +206,9 @@ def test_categorical_nan_only_columns(setup_path):
('col=="a"', DataFrame({"col": ["a", "b", "s"]}), DataFrame({"col": ["a"]})),
],
)
def test_convert_value(setup_path, where: str, df: DataFrame, expected: DataFrame):
def test_convert_value(
tmp_path, setup_path, where: str, df: DataFrame, expected: DataFrame
):
# GH39420
# Check that read_hdf with categorical columns can filter by where condition.
df.col = df.col.astype("category")
Expand All @@ -216,7 +217,7 @@ def test_convert_value(setup_path, where: str, df: DataFrame, expected: DataFram
expected.col = expected.col.astype("category")
expected.col = expected.col.cat.set_categories(categorical_values)

with ensure_clean_path(setup_path) as path:
df.to_hdf(path, "df", format="table", min_itemsize=max_widths)
result = read_hdf(path, where=where)
tm.assert_frame_equal(result, expected)
path = tmp_path / setup_path
df.to_hdf(path, "df", format="table", min_itemsize=max_widths)
result = read_hdf(path, where=where)
tm.assert_frame_equal(result, expected)
20 changes: 9 additions & 11 deletions pandas/tests/io/pytables/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import pandas as pd
import pandas._testing as tm
from pandas.tests.io.pytables.common import ensure_clean_path

tables = pytest.importorskip("tables")


@pytest.fixture
def pytables_hdf5_file():
def pytables_hdf5_file(tmp_path):
"""
Use PyTables to create a simple HDF5 file.
"""
Expand All @@ -29,16 +28,15 @@ def pytables_hdf5_file():

objname = "pandas_test_timeseries"

with ensure_clean_path("written_with_pytables.h5") as path:
# The `ensure_clean_path` context mgr removes the temp file upon exit.
with tables.open_file(path, mode="w") as f:
t = f.create_table("/", name=objname, description=table_schema)
for sample in testsamples:
for key, value in sample.items():
t.row[key] = value
t.row.append()
path = tmp_path / "written_with_pytables.h5"
with tables.open_file(path, mode="w") as f:
t = f.create_table("/", name=objname, description=table_schema)
for sample in testsamples:
for key, value in sample.items():
t.row[key] = value
t.row.append()

yield path, objname, pd.DataFrame(testsamples)
yield path, objname, pd.DataFrame(testsamples)


class TestReadPyTablesHDF5:
Expand Down
Loading