Skip to content
Open
Changes from all 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
53 changes: 26 additions & 27 deletions pandas/tests/io/pytables/test_select.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_store,
)

from pandas.io.pytables import Term
Expand All @@ -45,7 +44,7 @@ def test_select_columns_in_where(setup_path):
columns=["A", "B", "C"],
)

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
store.put("df", df, format="table")
expected = df[["A"]]

Expand All @@ -55,7 +54,7 @@ def test_select_columns_in_where(setup_path):

# With a Series
s = Series(np.random.default_rng(2).standard_normal(10), index=index, name="A")
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
store.put("s", s, format="table")
tm.assert_series_equal(store.select("s", where="columns=['A']"), s)

Expand All @@ -67,7 +66,7 @@ def test_select_with_dups(setup_path):
)
df.index = date_range("20130101 9:30", periods=10, freq="min", unit="ns")

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
store.append("df", df)

result = store.select("df")
Expand Down Expand Up @@ -98,7 +97,7 @@ def test_select_with_dups(setup_path):
)
df.index = date_range("20130101 9:30", periods=10, freq="min", unit="ns")

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
store.append("df", df)

result = store.select("df")
Expand All @@ -118,7 +117,7 @@ def test_select_with_dups(setup_path):
tm.assert_frame_equal(result, expected, by_blocks=True)

# duplicates on both index and columns
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
store.append("df", df)
store.append("df", df)

Expand All @@ -129,7 +128,7 @@ def test_select_with_dups(setup_path):


def test_select(setup_path):
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
# select with columns=
df = DataFrame(
np.random.default_rng(2).standard_normal((10, 4)),
Expand Down Expand Up @@ -170,7 +169,7 @@ def test_select(setup_path):


def test_select_dtypes(setup_path, request):
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
# with a Timestamp data column (GH #2637)
df = DataFrame(
{
Expand Down Expand Up @@ -232,7 +231,7 @@ def test_select_dtypes(setup_path, request):
expected = df.reindex(index=list(df.index)[0:10], columns=["A"])
tm.assert_frame_equal(expected, result)

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
# floats w/o NaN
df = DataFrame({"cols": range(11), "values": range(11)}, dtype="float64")
df["cols"] = (df["cols"] + 10).apply(str)
Expand Down Expand Up @@ -270,7 +269,7 @@ def test_select_dtypes(setup_path, request):

# test selection with comparison against numpy scalar
# GH 11283
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD")),
Expand All @@ -293,7 +292,7 @@ def test_select_dtypes(setup_path, request):


def test_select_with_many_inputs(setup_path):
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path, mode="a") as store:
df = DataFrame(
{
"ts": bdate_range("2012-01-01", periods=300, unit="ns"),
Expand Down Expand Up @@ -342,7 +341,7 @@ def test_select_with_many_inputs(setup_path):

def test_select_iterator(tmp_path, setup_path):
# single table
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
df = DataFrame(
np.random.default_rng(2).standard_normal((10, 4)),
columns=Index(list("ABCD")),
Expand Down Expand Up @@ -400,7 +399,7 @@ def test_select_iterator(tmp_path, setup_path):

# multiple

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
df1 = DataFrame(
np.random.default_rng(2).standard_normal((10, 4)),
columns=Index(list("ABCD")),
Expand Down Expand Up @@ -428,7 +427,7 @@ def test_select_iterator_complete_8014(setup_path):
chunksize = 1e4

# no iterator
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
expected = DataFrame(
np.random.default_rng(2).standard_normal((100064, 4)),
columns=Index(list("ABCD")),
Expand Down Expand Up @@ -463,7 +462,7 @@ def test_select_iterator_complete_8014(setup_path):
tm.assert_frame_equal(expected, result)

# with iterator, full range
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
expected = DataFrame(
np.random.default_rng(2).standard_normal((100064, 4)),
columns=Index(list("ABCD")),
Expand Down Expand Up @@ -505,7 +504,7 @@ def test_select_iterator_non_complete_8014(setup_path):
chunksize = 1e4

# with iterator, non complete range
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
expected = DataFrame(
np.random.default_rng(2).standard_normal((100064, 4)),
columns=Index(list("ABCD")),
Expand Down Expand Up @@ -539,7 +538,7 @@ def test_select_iterator_non_complete_8014(setup_path):
tm.assert_frame_equal(rexpected, result)

# with iterator, empty where
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
expected = DataFrame(
np.random.default_rng(2).standard_normal((100064, 4)),
columns=Index(list("ABCD")),
Expand All @@ -563,7 +562,7 @@ def test_select_iterator_many_empty_frames(setup_path):
chunksize = 10_000

# with iterator, range limited to the first chunk
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
expected = DataFrame(
np.random.default_rng(2).standard_normal((100064, 4)),
columns=Index(list("ABCD")),
Expand Down Expand Up @@ -622,7 +621,7 @@ def test_frame_select(setup_path, request):
index=date_range("2000-01-01", periods=10, freq="B", unit="ns"),
)

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
store.put("frame", df, format="table")
date = df.index[len(df) // 2]

Expand Down Expand Up @@ -675,7 +674,7 @@ def test_frame_select_complex(setup_path):
df["string"] = "foo"
df.loc[df.index[0:4], "string"] = "bar"

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
store.put("df", df, format="table", data_columns=["string"])

# empty
Expand Down Expand Up @@ -790,7 +789,7 @@ def test_invalid_filtering(setup_path):
index=date_range("2000-01-01", periods=10, freq="B", unit="ns"),
)

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
store.put("df", df, format="table")

msg = "unable to collapse Joint Filters"
Expand All @@ -805,7 +804,7 @@ def test_invalid_filtering(setup_path):

def test_string_select(setup_path):
# GH 2973
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
df = DataFrame(
np.random.default_rng(2).standard_normal((10, 4)),
columns=Index(list("ABCD")),
Expand Down Expand Up @@ -858,7 +857,7 @@ def test_select_as_multiple(setup_path):
df2 = df1.copy().rename(columns="{}_2".format)
df2["foo"] = "bar"

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
msg = "keys must be a list/tuple"
# no tables stored
with pytest.raises(TypeError, match=msg):
Expand Down Expand Up @@ -925,7 +924,7 @@ def test_select_as_multiple(setup_path):


def test_nan_selection_bug_4858(setup_path):
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
df = DataFrame({"cols": range(6), "values": range(6)}, dtype="float64")
df["cols"] = (df["cols"] + 10).apply(str)
df.iloc[0] = np.nan
Expand All @@ -949,7 +948,7 @@ def test_query_with_nested_special_character(setup_path):
}
)
expected = df[df.a == "test & test"]
with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
store.append("test", df, format="table", data_columns=True)
result = store.select("test", 'a = "test & test"')
tm.assert_frame_equal(expected, result)
Expand All @@ -959,7 +958,7 @@ def test_query_long_float_literal(setup_path):
# GH 14241
df = DataFrame({"A": [1000000000.0009, 1000000000.0011, 1000000000.0015]})

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
store.append("test", df, format="table", data_columns=True)

cutoff = 1000000000.0006
Expand Down Expand Up @@ -989,7 +988,7 @@ def test_query_compare_column_type(setup_path):
columns=["date", "real_date", "float", "int"],
)

with ensure_clean_store(setup_path) as store:
with HDFStore(setup_path) as store:
store.append("test", df, format="table", data_columns=True)

ts = Timestamp("2014-01-01") # noqa: F841
Expand Down
Loading