Skip to content

Commit 9342deb

Browse files
TST: Replace ensure_clean_store with tmp_path in test_select.py #62435
1 parent bc458ec commit 9342deb

File tree

1 file changed

+72
-49
lines changed

1 file changed

+72
-49
lines changed

pandas/tests/io/pytables/test_select.py

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
)
2121
from pandas.tests.io.pytables.common import (
2222
_maybe_remove,
23-
ensure_clean_store,
2423
)
2524

2625
from pandas.io.pytables import Term
2726

2827
pytestmark = [pytest.mark.single_cpu]
2928

3029

31-
def test_select_columns_in_where(setup_path):
30+
def test_select_columns_in_where(tmp_path):
3231
# GH 6169
3332
# recreate multi-indexes when columns is passed
3433
# in the `where` argument
@@ -44,8 +43,8 @@ def test_select_columns_in_where(setup_path):
4443
index=index,
4544
columns=["A", "B", "C"],
4645
)
47-
48-
with ensure_clean_store(setup_path) as store:
46+
path = tmp_path / "file.h5"
47+
with pd.HDFStore(path) as store:
4948
store.put("df", df, format="table")
5049
expected = df[["A"]]
5150

@@ -55,19 +54,21 @@ def test_select_columns_in_where(setup_path):
5554

5655
# With a Series
5756
s = Series(np.random.default_rng(2).standard_normal(10), index=index, name="A")
58-
with ensure_clean_store(setup_path) as store:
57+
path = tmp_path / "file.h6"
58+
with pd.HDFStore(path) as store:
5959
store.put("s", s, format="table")
6060
tm.assert_series_equal(store.select("s", where="columns=['A']"), s)
6161

6262

63-
def test_select_with_dups(setup_path):
63+
def test_select_with_dups(tmp_path):
6464
# single dtypes
6565
df = DataFrame(
6666
np.random.default_rng(2).standard_normal((10, 4)), columns=["A", "A", "B", "B"]
6767
)
6868
df.index = date_range("20130101 9:30", periods=10, freq="min")
6969

70-
with ensure_clean_store(setup_path) as store:
70+
path = tmp_path / "file.h7"
71+
with pd.HDFStore(path) as store:
7172
store.append("df", df)
7273

7374
result = store.select("df")
@@ -97,8 +98,8 @@ def test_select_with_dups(setup_path):
9798
axis=1,
9899
)
99100
df.index = date_range("20130101 9:30", periods=10, freq="min")
100-
101-
with ensure_clean_store(setup_path) as store:
101+
path = tmp_path / "file.h8"
102+
with pd.HDFStore(path) as store:
102103
store.append("df", df)
103104

104105
result = store.select("df")
@@ -118,7 +119,8 @@ def test_select_with_dups(setup_path):
118119
tm.assert_frame_equal(result, expected, by_blocks=True)
119120

120121
# duplicates on both index and columns
121-
with ensure_clean_store(setup_path) as store:
122+
path = tmp_path / "file.h9"
123+
with pd.HDFStore(path) as store:
122124
store.append("df", df)
123125
store.append("df", df)
124126

@@ -128,8 +130,9 @@ def test_select_with_dups(setup_path):
128130
tm.assert_frame_equal(result, expected, by_blocks=True)
129131

130132

131-
def test_select(setup_path):
132-
with ensure_clean_store(setup_path) as store:
133+
def test_select(tmp_path):
134+
path = tmp_path / "file.h10"
135+
with pd.HDFStore(path) as store:
133136
# select with columns=
134137
df = DataFrame(
135138
np.random.default_rng(2).standard_normal((10, 4)),
@@ -169,8 +172,9 @@ def test_select(setup_path):
169172
tm.assert_frame_equal(expected, result)
170173

171174

172-
def test_select_dtypes(setup_path, request):
173-
with ensure_clean_store(setup_path) as store:
175+
def test_select_dtypes(tmp_path, request):
176+
path = tmp_path / "file.h11"
177+
with pd.HDFStore(path) as store:
174178
# with a Timestamp data column (GH #2637)
175179
df = DataFrame(
176180
{
@@ -232,7 +236,8 @@ def test_select_dtypes(setup_path, request):
232236
expected = df.reindex(index=list(df.index)[0:10], columns=["A"])
233237
tm.assert_frame_equal(expected, result)
234238

235-
with ensure_clean_store(setup_path) as store:
239+
path = tmp_path / "file.h12"
240+
with pd.HDFStore(path) as store:
236241
# floats w/o NaN
237242
df = DataFrame({"cols": range(11), "values": range(11)}, dtype="float64")
238243
df["cols"] = (df["cols"] + 10).apply(str)
@@ -270,7 +275,8 @@ def test_select_dtypes(setup_path, request):
270275

271276
# test selection with comparison against numpy scalar
272277
# GH 11283
273-
with ensure_clean_store(setup_path) as store:
278+
path = tmp_path / "file.h13"
279+
with pd.HDFStore(path) as store:
274280
df = DataFrame(
275281
1.1 * np.arange(120).reshape((30, 4)),
276282
columns=Index(list("ABCD")),
@@ -292,8 +298,9 @@ def test_select_dtypes(setup_path, request):
292298
tm.assert_frame_equal(expected, result)
293299

294300

295-
def test_select_with_many_inputs(setup_path):
296-
with ensure_clean_store(setup_path) as store:
301+
def test_select_with_many_inputs(tmp_path):
302+
path = tmp_path / "file.h14"
303+
with pd.HDFStore(path) as store:
297304
df = DataFrame(
298305
{
299306
"ts": bdate_range("2012-01-01", periods=300),
@@ -340,9 +347,10 @@ def test_select_with_many_inputs(setup_path):
340347
assert len(result) == 100
341348

342349

343-
def test_select_iterator(tmp_path, setup_path):
350+
def test_select_iterator(tmp_path):
344351
# single table
345-
with ensure_clean_store(setup_path) as store:
352+
path = tmp_path / "file.h15"
353+
with pd.HDFStore(path) as store:
346354
df = DataFrame(
347355
np.random.default_rng(2).standard_normal((10, 4)),
348356
columns=Index(list("ABCD")),
@@ -366,7 +374,7 @@ def test_select_iterator(tmp_path, setup_path):
366374
result = concat(results)
367375
tm.assert_frame_equal(result, expected)
368376

369-
path = tmp_path / setup_path
377+
path = tmp_path / "file.h15"
370378

371379
df = DataFrame(
372380
np.random.default_rng(2).standard_normal((10, 4)),
@@ -382,7 +390,7 @@ def test_select_iterator(tmp_path, setup_path):
382390
with pytest.raises(TypeError, match=msg):
383391
read_hdf(path, "df_non_table", iterator=True)
384392

385-
path = tmp_path / setup_path
393+
path = tmp_path / "file.h15"
386394

387395
df = DataFrame(
388396
np.random.default_rng(2).standard_normal((10, 4)),
@@ -400,7 +408,8 @@ def test_select_iterator(tmp_path, setup_path):
400408

401409
# multiple
402410

403-
with ensure_clean_store(setup_path) as store:
411+
path = tmp_path / "file.h16"
412+
with pd.HDFStore(path) as store:
404413
df1 = DataFrame(
405414
np.random.default_rng(2).standard_normal((10, 4)),
406415
columns=Index(list("ABCD")),
@@ -422,13 +431,14 @@ def test_select_iterator(tmp_path, setup_path):
422431
tm.assert_frame_equal(expected, result)
423432

424433

425-
def test_select_iterator_complete_8014(setup_path):
434+
def test_select_iterator_complete_8014(tmp_path):
426435
# GH 8014
427436
# using iterator and where clause
428437
chunksize = 1e4
429438

430439
# no iterator
431-
with ensure_clean_store(setup_path) as store:
440+
path = tmp_path / "file.h17"
441+
with pd.HDFStore(path) as store:
432442
expected = DataFrame(
433443
np.random.default_rng(2).standard_normal((100064, 4)),
434444
columns=Index(list("ABCD")),
@@ -463,7 +473,8 @@ def test_select_iterator_complete_8014(setup_path):
463473
tm.assert_frame_equal(expected, result)
464474

465475
# with iterator, full range
466-
with ensure_clean_store(setup_path) as store:
476+
path = tmp_path / "file.h18"
477+
with pd.HDFStore(path) as store:
467478
expected = DataFrame(
468479
np.random.default_rng(2).standard_normal((100064, 4)),
469480
columns=Index(list("ABCD")),
@@ -499,13 +510,14 @@ def test_select_iterator_complete_8014(setup_path):
499510
tm.assert_frame_equal(expected, result)
500511

501512

502-
def test_select_iterator_non_complete_8014(setup_path):
513+
def test_select_iterator_non_complete_8014(tmp_path):
503514
# GH 8014
504515
# using iterator and where clause
505516
chunksize = 1e4
506517

507518
# with iterator, non complete range
508-
with ensure_clean_store(setup_path) as store:
519+
path = tmp_path / "file.h19"
520+
with pd.HDFStore(path) as store:
509521
expected = DataFrame(
510522
np.random.default_rng(2).standard_normal((100064, 4)),
511523
columns=Index(list("ABCD")),
@@ -539,7 +551,8 @@ def test_select_iterator_non_complete_8014(setup_path):
539551
tm.assert_frame_equal(rexpected, result)
540552

541553
# with iterator, empty where
542-
with ensure_clean_store(setup_path) as store:
554+
path = tmp_path / "file.h20"
555+
with pd.HDFStore(path) as store:
543556
expected = DataFrame(
544557
np.random.default_rng(2).standard_normal((100064, 4)),
545558
columns=Index(list("ABCD")),
@@ -556,14 +569,15 @@ def test_select_iterator_non_complete_8014(setup_path):
556569
assert 0 == len(results)
557570

558571

559-
def test_select_iterator_many_empty_frames(setup_path):
572+
def test_select_iterator_many_empty_frames(tmp_path):
560573
# GH 8014
561574
# using iterator and where clause can return many empty
562575
# frames.
563576
chunksize = 10_000
564577

565578
# with iterator, range limited to the first chunk
566-
with ensure_clean_store(setup_path) as store:
579+
path = tmp_path / "file.h21"
580+
with pd.HDFStore(path) as store:
567581
expected = DataFrame(
568582
np.random.default_rng(2).standard_normal((100064, 4)),
569583
columns=Index(list("ABCD")),
@@ -615,14 +629,15 @@ def test_select_iterator_many_empty_frames(setup_path):
615629
assert len(results) == 0
616630

617631

618-
def test_frame_select(setup_path, request):
632+
def test_frame_select(tmp_path, request):
619633
df = DataFrame(
620634
np.random.default_rng(2).standard_normal((10, 4)),
621635
columns=Index(list("ABCD")),
622636
index=date_range("2000-01-01", periods=10, freq="B"),
623637
)
624638

625-
with ensure_clean_store(setup_path) as store:
639+
path = tmp_path / "file.h22"
640+
with pd.HDFStore(path) as store:
626641
store.put("frame", df, format="table")
627642
date = df.index[len(df) // 2]
628643

@@ -664,7 +679,7 @@ def test_frame_select(setup_path, request):
664679
# store.select('frame', [crit1, crit2])
665680

666681

667-
def test_frame_select_complex(setup_path):
682+
def test_frame_select_complex(tmp_path):
668683
# select via complex criteria
669684

670685
df = DataFrame(
@@ -675,7 +690,8 @@ def test_frame_select_complex(setup_path):
675690
df["string"] = "foo"
676691
df.loc[df.index[0:4], "string"] = "bar"
677692

678-
with ensure_clean_store(setup_path) as store:
693+
path = tmp_path / "file.h23"
694+
with pd.HDFStore(path) as store:
679695
store.put("df", df, format="table", data_columns=["string"])
680696

681697
# empty
@@ -781,7 +797,7 @@ def test_frame_select_complex2(tmp_path):
781797
tm.assert_frame_equal(result, expected)
782798

783799

784-
def test_invalid_filtering(setup_path):
800+
def test_invalid_filtering(tmp_path):
785801
# can't use more than one filter (atm)
786802

787803
df = DataFrame(
@@ -790,7 +806,8 @@ def test_invalid_filtering(setup_path):
790806
index=date_range("2000-01-01", periods=10, freq="B"),
791807
)
792808

793-
with ensure_clean_store(setup_path) as store:
809+
path = tmp_path / "file.h24"
810+
with pd.HDFStore(path) as store:
794811
store.put("df", df, format="table")
795812

796813
msg = "unable to collapse Joint Filters"
@@ -803,9 +820,10 @@ def test_invalid_filtering(setup_path):
803820
store.select("df", "columns=['A','B'] & columns=['C']")
804821

805822

806-
def test_string_select(setup_path):
823+
def test_string_select(tmp_path):
807824
# GH 2973
808-
with ensure_clean_store(setup_path) as store:
825+
path = tmp_path / "file.h25"
826+
with pd.HDFStore(path) as store:
809827
df = DataFrame(
810828
np.random.default_rng(2).standard_normal((10, 4)),
811829
columns=Index(list("ABCD")),
@@ -849,7 +867,7 @@ def test_string_select(setup_path):
849867
tm.assert_frame_equal(result, expected)
850868

851869

852-
def test_select_as_multiple(setup_path):
870+
def test_select_as_multiple(tmp_path):
853871
df1 = DataFrame(
854872
np.random.default_rng(2).standard_normal((10, 4)),
855873
columns=Index(list("ABCD")),
@@ -858,7 +876,8 @@ def test_select_as_multiple(setup_path):
858876
df2 = df1.copy().rename(columns="{}_2".format)
859877
df2["foo"] = "bar"
860878

861-
with ensure_clean_store(setup_path) as store:
879+
path = tmp_path / "file.h26"
880+
with pd.HDFStore(path) as store:
862881
msg = "keys must be a list/tuple"
863882
# no tables stored
864883
with pytest.raises(TypeError, match=msg):
@@ -924,8 +943,9 @@ def test_select_as_multiple(setup_path):
924943
)
925944

926945

927-
def test_nan_selection_bug_4858(setup_path):
928-
with ensure_clean_store(setup_path) as store:
946+
def test_nan_selection_bug_4858(tmp_path):
947+
path = tmp_path / "file.h27"
948+
with pd.HDFStore(path) as store:
929949
df = DataFrame({"cols": range(6), "values": range(6)}, dtype="float64")
930950
df["cols"] = (df["cols"] + 10).apply(str)
931951
df.iloc[0] = np.nan
@@ -941,25 +961,27 @@ def test_nan_selection_bug_4858(setup_path):
941961
tm.assert_frame_equal(result, expected)
942962

943963

944-
def test_query_with_nested_special_character(setup_path):
964+
def test_query_with_nested_special_character(tmp_path):
945965
df = DataFrame(
946966
{
947967
"a": ["a", "a", "c", "b", "test & test", "c", "b", "e"],
948968
"b": [1, 2, 3, 4, 5, 6, 7, 8],
949969
}
950970
)
951971
expected = df[df.a == "test & test"]
952-
with ensure_clean_store(setup_path) as store:
972+
path = tmp_path / "file.h28"
973+
with pd.HDFStore(path) as store:
953974
store.append("test", df, format="table", data_columns=True)
954975
result = store.select("test", 'a = "test & test"')
955976
tm.assert_frame_equal(expected, result)
956977

957978

958-
def test_query_long_float_literal(setup_path):
979+
def test_query_long_float_literal(tmp_path):
959980
# GH 14241
960981
df = DataFrame({"A": [1000000000.0009, 1000000000.0011, 1000000000.0015]})
961982

962-
with ensure_clean_store(setup_path) as store:
983+
path = tmp_path / "file.h29"
984+
with pd.HDFStore(path) as store:
963985
store.append("test", df, format="table", data_columns=True)
964986

965987
cutoff = 1000000000.0006
@@ -977,7 +999,7 @@ def test_query_long_float_literal(setup_path):
977999
tm.assert_frame_equal(expected, result)
9781000

9791001

980-
def test_query_compare_column_type(setup_path):
1002+
def test_query_compare_column_type(tmp_path):
9811003
# GH 15492
9821004
df = DataFrame(
9831005
{
@@ -989,7 +1011,8 @@ def test_query_compare_column_type(setup_path):
9891011
columns=["date", "real_date", "float", "int"],
9901012
)
9911013

992-
with ensure_clean_store(setup_path) as store:
1014+
path = tmp_path / "file.h30"
1015+
with pd.HDFStore(path) as store:
9931016
store.append("test", df, format="table", data_columns=True)
9941017

9951018
ts = Timestamp("2014-01-01") # noqa: F841

0 commit comments

Comments
 (0)