Skip to content

Commit 0049d66

Browse files
TST: Replace ensure_clean_store with tmp_path in tests/io/pytables/test_append.py
1 parent a505423 commit 0049d66

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

pandas/tests/io/pytables/test_append.py

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pandas as pd
1212
from pandas import (
1313
DataFrame,
14+
HDFStore,
1415
Index,
1516
Series,
1617
_testing as tm,
@@ -20,7 +21,6 @@
2021
)
2122
from pandas.tests.io.pytables.common import (
2223
_maybe_remove,
23-
ensure_clean_store,
2424
)
2525

2626
pytestmark = [pytest.mark.single_cpu]
@@ -29,8 +29,9 @@
2929

3030

3131
@pytest.mark.filterwarnings("ignore::tables.NaturalNameWarning")
32-
def test_append(setup_path):
33-
with ensure_clean_store(setup_path) as store:
32+
def test_append(tmp_path):
33+
path = tmp_path / "test_append.h5"
34+
with HDFStore(path) as store:
3435
# this is allowed by almost always don't want to do it
3536
# tables.NaturalNameWarning):
3637
df = DataFrame(
@@ -101,8 +102,9 @@ def test_append(setup_path):
101102
tm.assert_frame_equal(store["uints"], uint_data, check_index_type=True)
102103

103104

104-
def test_append_series(setup_path):
105-
with ensure_clean_store(setup_path) as store:
105+
def test_append_series(tmp_path):
106+
path = tmp_path / "test_append_series.h5"
107+
with HDFStore(path) as store:
106108
# basic
107109
ss = Series(range(20), dtype=np.float64, index=[f"i_{i}" for i in range(20)])
108110
ts = Series(
@@ -150,8 +152,9 @@ def test_append_series(setup_path):
150152
tm.assert_series_equal(store["mi"], s, check_index_type=True)
151153

152154

153-
def test_append_some_nans(setup_path):
154-
with ensure_clean_store(setup_path) as store:
155+
def test_append_some_nans(tmp_path):
156+
path = tmp_path / "test_append_some_nans.h5"
157+
with HDFStore(path) as store:
155158
df = DataFrame(
156159
{
157160
"A": Series(np.random.default_rng(2).standard_normal(20)).astype(
@@ -198,8 +201,9 @@ def test_append_some_nans(setup_path):
198201
tm.assert_frame_equal(store["df3"], df3, check_index_type=True)
199202

200203

201-
def test_append_all_nans(setup_path, using_infer_string):
202-
with ensure_clean_store(setup_path) as store:
204+
def test_append_all_nans(tmp_path, using_infer_string):
205+
path = tmp_path / "test_append_all_nans.h5"
206+
with HDFStore(path) as store:
203207
df = DataFrame(
204208
{
205209
"A1": np.random.default_rng(2).standard_normal(20),
@@ -290,8 +294,9 @@ def test_append_all_nans(setup_path, using_infer_string):
290294
tm.assert_frame_equal(store["df2"], df, check_index_type=True)
291295

292296

293-
def test_append_frame_column_oriented(setup_path, request):
294-
with ensure_clean_store(setup_path) as store:
297+
def test_append_frame_column_oriented(tmp_path, request):
298+
path = tmp_path / "test_append_frame_column_oriented.h5"
299+
with HDFStore(path) as store:
295300
# column oriented
296301
df = DataFrame(
297302
np.random.default_rng(2).standard_normal((10, 4)),
@@ -330,9 +335,10 @@ def test_append_frame_column_oriented(setup_path, request):
330335
store.select("df1", "columns=A and index>df.index[4]")
331336

332337

333-
def test_append_with_different_block_ordering(setup_path):
338+
def test_append_with_different_block_ordering(tmp_path):
334339
# GH 4096; using same frames, but different block orderings
335-
with ensure_clean_store(setup_path) as store:
340+
path = tmp_path / "test_append_with_different_block_ordering.h5"
341+
with HDFStore(path) as store:
336342
for i in range(10):
337343
df = DataFrame(
338344
np.random.default_rng(2).standard_normal((10, 2)), columns=list("AB")
@@ -355,14 +361,15 @@ def test_append_with_different_block_ordering(setup_path):
355361

356362
# test a different ordering but with more fields (like invalid
357363
# combinations)
358-
with ensure_clean_store(setup_path) as store:
364+
with HDFStore(path) as store:
359365
df = DataFrame(
360366
np.random.default_rng(2).standard_normal((10, 2)),
361367
columns=list("AB"),
362368
dtype="float64",
363369
)
364370
df["int64"] = Series([1] * len(df), dtype="int64")
365371
df["int16"] = Series([1] * len(df), dtype="int16")
372+
_maybe_remove(store, "df")
366373
store.append("df", df)
367374

368375
# store additional fields in different blocks
@@ -382,8 +389,9 @@ def test_append_with_different_block_ordering(setup_path):
382389
store.append("df", df)
383390

384391

385-
def test_append_with_strings(setup_path):
386-
with ensure_clean_store(setup_path) as store:
392+
def test_append_with_strings(tmp_path):
393+
path = tmp_path / "test_append_with_strings.h5"
394+
with HDFStore(path) as store:
387395

388396
def check_col(key, name, size):
389397
assert (
@@ -467,7 +475,7 @@ def check_col(key, name, size):
467475
result = store.select("df")
468476
tm.assert_frame_equal(result, df)
469477

470-
with ensure_clean_store(setup_path) as store:
478+
with HDFStore(path) as store:
471479
df = DataFrame({"A": "foo", "B": "bar"}, index=range(10))
472480

473481
# a min_itemsize that creates a data_column
@@ -505,17 +513,19 @@ def check_col(key, name, size):
505513
store.append("df", df, min_itemsize={"foo": 20, "foobar": 20})
506514

507515

508-
def test_append_with_empty_string(setup_path):
509-
with ensure_clean_store(setup_path) as store:
516+
def test_append_with_empty_string(tmp_path):
517+
path = tmp_path / "test_append_with_empty_string.h5"
518+
with HDFStore(path) as store:
510519
# with all empty strings (GH 12242)
511520
df = DataFrame({"x": ["a", "b", "c", "d", "e", "f", ""]})
512521
store.append("df", df[:-1], min_itemsize={"x": 1})
513522
store.append("df", df[-1:], min_itemsize={"x": 1})
514523
tm.assert_frame_equal(store.select("df"), df)
515524

516525

517-
def test_append_with_data_columns(setup_path):
518-
with ensure_clean_store(setup_path) as store:
526+
def test_append_with_data_columns(tmp_path):
527+
path = tmp_path / "test_append_with_data_columns.h5"
528+
with HDFStore(path) as store:
519529
df = DataFrame(
520530
np.random.default_rng(2).standard_normal((10, 4)),
521531
columns=Index(list("ABCD")),
@@ -559,7 +569,7 @@ def check_col(key, name, size):
559569
getattr(store.get_storer(key).table.description, name).itemsize == size
560570
)
561571

562-
with ensure_clean_store(setup_path) as store:
572+
with HDFStore(path) as store:
563573
_maybe_remove(store, "df")
564574
store.append("df", df_new, data_columns=["string"], min_itemsize={"string": 30})
565575
check_col("df", "string", 30)
@@ -570,7 +580,7 @@ def check_col(key, name, size):
570580
store.append("df", df_new, data_columns=["string"], min_itemsize={"values": 30})
571581
check_col("df", "string", 30)
572582

573-
with ensure_clean_store(setup_path) as store:
583+
with HDFStore(path) as store:
574584
df_new["string2"] = "foobarbah"
575585
df_new["string_block1"] = "foobarbah1"
576586
df_new["string_block2"] = "foobarbah2"
@@ -585,7 +595,7 @@ def check_col(key, name, size):
585595
check_col("df", "string2", 40)
586596
check_col("df", "values_block_1", 50)
587597

588-
with ensure_clean_store(setup_path) as store:
598+
with HDFStore(path) as store:
589599
# multiple data columns
590600
df_new = df.copy()
591601
df_new.iloc[0, df_new.columns.get_loc("A")] = 1.0
@@ -617,7 +627,7 @@ def check_col(key, name, size):
617627
expected = df_new[(df_new.string == "foo") & (df_new.string2 == "cool")]
618628
tm.assert_frame_equal(result, expected)
619629

620-
with ensure_clean_store(setup_path) as store:
630+
with HDFStore(path) as store:
621631
# doc example
622632
df_dc = df.copy()
623633
df_dc["string"] = "foo"
@@ -642,7 +652,7 @@ def check_col(key, name, size):
642652
# FIXME: 2020-12-07 intermittent build failures here with freq of
643653
# None instead of BDay(4)
644654

645-
with ensure_clean_store(setup_path) as store:
655+
with HDFStore(path) as store:
646656
# doc example part 2
647657

648658
index = date_range("1/1/2000", periods=8)
@@ -658,6 +668,7 @@ def check_col(key, name, size):
658668
df_dc["string2"] = "cool"
659669

660670
# on-disk operations
671+
_maybe_remove(store, "df_dc")
661672
store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"])
662673

663674
result = store.select("df_dc", "B>0")
@@ -669,11 +680,12 @@ def check_col(key, name, size):
669680
tm.assert_frame_equal(result, expected)
670681

671682

672-
def test_append_hierarchical(tmp_path, setup_path, multiindex_dataframe_random_data):
683+
def test_append_hierarchical(tmp_path, multiindex_dataframe_random_data):
673684
df = multiindex_dataframe_random_data
674685
df.columns.name = None
675686

676-
with ensure_clean_store(setup_path) as store:
687+
path = tmp_path / "test_append_hierarchical.h5"
688+
with HDFStore(path) as store:
677689
store.append("mi", df)
678690
result = store.select("mi")
679691
tm.assert_frame_equal(result, df)
@@ -690,8 +702,9 @@ def test_append_hierarchical(tmp_path, setup_path, multiindex_dataframe_random_d
690702
tm.assert_frame_equal(result, expected)
691703

692704

693-
def test_append_misc(setup_path):
694-
with ensure_clean_store(setup_path) as store:
705+
def test_append_misc(tmp_path):
706+
path = tmp_path / "test_append_misc.h5"
707+
with HDFStore(path) as store:
695708
df = DataFrame(
696709
1.1 * np.arange(120).reshape((30, 4)),
697710
columns=Index(list("ABCD")),
@@ -707,7 +720,7 @@ def test_append_misc(setup_path):
707720

708721

709722
@pytest.mark.parametrize("chunksize", [10, 200, 1000])
710-
def test_append_misc_chunksize(setup_path, chunksize):
723+
def test_append_misc_chunksize(tmp_path, chunksize):
711724
# more chunksize in append tests
712725
df = DataFrame(
713726
1.1 * np.arange(120).reshape((30, 4)),
@@ -720,15 +733,17 @@ def test_append_misc_chunksize(setup_path, chunksize):
720733
df["bool"] = df["float322"] > 0
721734
df["time1"] = Timestamp("20130101").as_unit("ns")
722735
df["time2"] = Timestamp("20130102").as_unit("ns")
723-
with ensure_clean_store(setup_path, mode="w") as store:
736+
path = tmp_path / "test_append_misc_chunksize.h5"
737+
with HDFStore(path) as store:
724738
store.append("obj", df, chunksize=chunksize)
725739
result = store.select("obj")
726740
tm.assert_frame_equal(result, df)
727741

728742

729-
def test_append_misc_empty_frame(setup_path):
743+
def test_append_misc_empty_frame(tmp_path):
744+
path = tmp_path / "test_append_misc_empty_frame.h5"
730745
# empty frame, GH4273
731-
with ensure_clean_store(setup_path) as store:
746+
with HDFStore(path) as store:
732747
# 0 len
733748
df_empty = DataFrame(columns=list("ABC"))
734749
store.append("df", df_empty)
@@ -748,8 +763,9 @@ def test_append_misc_empty_frame(setup_path):
748763
tm.assert_frame_equal(store.select("df2"), df)
749764

750765

751-
def test_append_raise(setup_path, using_infer_string):
752-
with ensure_clean_store(setup_path) as store:
766+
def test_append_raise(tmp_path, using_infer_string):
767+
path = tmp_path / "test_append_raise.h5"
768+
with HDFStore(path) as store:
753769
# test append with invalid input to get good error messages
754770

755771
# list in column
@@ -832,7 +848,7 @@ def test_append_raise(setup_path, using_infer_string):
832848
store.append("df", df)
833849

834850

835-
def test_append_with_timedelta(setup_path):
851+
def test_append_with_timedelta(tmp_path):
836852
# GH 3577
837853
# append timedelta
838854

@@ -846,7 +862,8 @@ def test_append_with_timedelta(setup_path):
846862
df["C"] = df["A"] - df["B"]
847863
df.loc[3:5, "C"] = np.nan
848864

849-
with ensure_clean_store(setup_path) as store:
865+
path = tmp_path / "test_append_with_timedelta.h5"
866+
with HDFStore(path) as store:
850867
# table
851868
_maybe_remove(store, "df")
852869
store.append("df", df, data_columns=True)
@@ -879,7 +896,7 @@ def test_append_with_timedelta(setup_path):
879896
tm.assert_frame_equal(result, df)
880897

881898

882-
def test_append_to_multiple(setup_path):
899+
def test_append_to_multiple(tmp_path):
883900
df1 = DataFrame(
884901
np.random.default_rng(2).standard_normal((10, 4)),
885902
columns=Index(list("ABCD")),
@@ -889,7 +906,8 @@ def test_append_to_multiple(setup_path):
889906
df2["foo"] = "bar"
890907
df = concat([df1, df2], axis=1)
891908

892-
with ensure_clean_store(setup_path) as store:
909+
path = tmp_path / "test_append_to_multiple.h5"
910+
with HDFStore(path) as store:
893911
# exceptions
894912
msg = "append_to_multiple requires a selector that is in passed dict"
895913
with pytest.raises(ValueError, match=msg):
@@ -916,7 +934,7 @@ def test_append_to_multiple(setup_path):
916934
tm.assert_frame_equal(result, expected)
917935

918936

919-
def test_append_to_multiple_dropna(setup_path):
937+
def test_append_to_multiple_dropna(tmp_path):
920938
df1 = DataFrame(
921939
np.random.default_rng(2).standard_normal((10, 4)),
922940
columns=Index(list("ABCD")),
@@ -930,7 +948,8 @@ def test_append_to_multiple_dropna(setup_path):
930948
df1.iloc[1, df1.columns.get_indexer(["A", "B"])] = np.nan
931949
df = concat([df1, df2], axis=1)
932950

933-
with ensure_clean_store(setup_path) as store:
951+
path = tmp_path / "test_append_to_multiple_dropna.h5"
952+
with HDFStore(path) as store:
934953
# dropna=True should guarantee rows are synchronized
935954
store.append_to_multiple(
936955
{"df1": ["A", "B"], "df2": None}, df, selector="df1", dropna=True
@@ -941,7 +960,7 @@ def test_append_to_multiple_dropna(setup_path):
941960
tm.assert_index_equal(store.select("df1").index, store.select("df2").index)
942961

943962

944-
def test_append_to_multiple_dropna_false(setup_path):
963+
def test_append_to_multiple_dropna_false(tmp_path):
945964
df1 = DataFrame(
946965
np.random.default_rng(2).standard_normal((10, 4)),
947966
columns=Index(list("ABCD")),
@@ -951,8 +970,9 @@ def test_append_to_multiple_dropna_false(setup_path):
951970
df1.iloc[1, df1.columns.get_indexer(["A", "B"])] = np.nan
952971
df = concat([df1, df2], axis=1)
953972

973+
path = tmp_path / "test_append_to_multiple_dropna_false.h5"
954974
with (
955-
ensure_clean_store(setup_path) as store,
975+
HDFStore(path) as store,
956976
pd.option_context("io.hdf.dropna_table", True),
957977
):
958978
# dropna=False shouldn't synchronize row indexes
@@ -967,7 +987,7 @@ def test_append_to_multiple_dropna_false(setup_path):
967987
assert not store.select("df1a").index.equals(store.select("df2a").index)
968988

969989

970-
def test_append_to_multiple_min_itemsize(setup_path):
990+
def test_append_to_multiple_min_itemsize(tmp_path):
971991
# GH 11238
972992
df = DataFrame(
973993
{
@@ -982,7 +1002,8 @@ def test_append_to_multiple_min_itemsize(setup_path):
9821002
# Reading/writing RangeIndex info is not supported yet
9831003
expected.index = Index(list(range(len(expected.index))))
9841004

985-
with ensure_clean_store(setup_path) as store:
1005+
path = tmp_path / "test_append_to_multiple_min_itemsize.h5"
1006+
with HDFStore(path) as store:
9861007
store.append_to_multiple(
9871008
{
9881009
"index": ["IX"],
@@ -997,14 +1018,15 @@ def test_append_to_multiple_min_itemsize(setup_path):
9971018
tm.assert_frame_equal(result, expected, check_index_type=True)
9981019

9991020

1000-
def test_append_string_nan_rep(setup_path):
1021+
def test_append_string_nan_rep(tmp_path):
10011022
# GH 16300
10021023
df = DataFrame({"A": "a", "B": "foo"}, index=np.arange(10))
10031024
df_nan = df.copy()
10041025
df_nan.loc[0:4, :] = np.nan
10051026
msg = "NaN representation is too large for existing column size"
10061027

1007-
with ensure_clean_store(setup_path) as store:
1028+
path = tmp_path / "test_append_string_nan_rep.h5"
1029+
with HDFStore(path) as store:
10081030
# string column too small
10091031
store.append("sa", df["A"])
10101032
with pytest.raises(ValueError, match=msg):

0 commit comments

Comments
 (0)