Skip to content

Commit 17e6cc3

Browse files
tolaa001Nico Cernek
authored and
Nico Cernek
committed
Test pytables refactor (pandas-dev#28746)
1 parent bd65f44 commit 17e6cc3

File tree

7 files changed

+711
-655
lines changed

7 files changed

+711
-655
lines changed

pandas/tests/io/pytables/common.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from contextlib import contextmanager
2+
import os
3+
import tempfile
4+
5+
import pytest
6+
7+
from pandas.io.pytables import HDFStore
8+
9+
tables = pytest.importorskip("tables")
10+
# set these parameters so we don't have file sharing
11+
tables.parameters.MAX_NUMEXPR_THREADS = 1
12+
tables.parameters.MAX_BLOSC_THREADS = 1
13+
tables.parameters.MAX_THREADS = 1
14+
15+
16+
def safe_remove(path):
17+
if path is not None:
18+
try:
19+
os.remove(path)
20+
except OSError:
21+
pass
22+
23+
24+
def safe_close(store):
25+
try:
26+
if store is not None:
27+
store.close()
28+
except IOError:
29+
pass
30+
31+
32+
def create_tempfile(path):
33+
""" create an unopened named temporary file """
34+
return os.path.join(tempfile.gettempdir(), path)
35+
36+
37+
# contextmanager to ensure the file cleanup
38+
@contextmanager
39+
def ensure_clean_store(path, mode="a", complevel=None, complib=None, fletcher32=False):
40+
41+
try:
42+
43+
# put in the temporary path if we don't have one already
44+
if not len(os.path.dirname(path)):
45+
path = create_tempfile(path)
46+
47+
store = HDFStore(
48+
path, mode=mode, complevel=complevel, complib=complib, fletcher32=False
49+
)
50+
yield store
51+
finally:
52+
safe_close(store)
53+
if mode == "w" or mode == "a":
54+
safe_remove(path)
55+
56+
57+
@contextmanager
58+
def ensure_clean_path(path):
59+
"""
60+
return essentially a named temporary file that is not opened
61+
and deleted on exiting; if path is a list, then create and
62+
return list of filenames
63+
"""
64+
try:
65+
if isinstance(path, list):
66+
filenames = [create_tempfile(p) for p in path]
67+
yield filenames
68+
else:
69+
filenames = [create_tempfile(path)]
70+
yield filenames[0]
71+
finally:
72+
for f in filenames:
73+
safe_remove(f)
74+
75+
76+
def _maybe_remove(store, key):
77+
"""For tests using tables, try removing the table to be sure there is
78+
no content from previous tests using the same table name."""
79+
try:
80+
store.remove(key)
81+
except (ValueError, KeyError):
82+
pass

pandas/tests/io/pytables/conftest.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
import pandas.util.testing as tm
4+
5+
6+
@pytest.fixture
7+
def setup_path():
8+
"""Fixture for setup path"""
9+
return "tmp.__{}__.h5".format(tm.rands(10))
10+
11+
12+
@pytest.fixture(scope="module", autouse=True)
13+
def setup_mode():
14+
""" Reset testing mode fixture"""
15+
tm.reset_testing_mode()
16+
yield
17+
tm.set_testing_mode()

pandas/tests/io/pytables/test_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
import pandas as pd
4-
from pandas.tests.io.pytables.test_pytables import ensure_clean_path
4+
from pandas.tests.io.pytables.common import ensure_clean_path
55
from pandas.util.testing import assert_frame_equal
66

77
tables = pytest.importorskip("tables")
+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
from warnings import catch_warnings
2+
3+
import numpy as np
4+
import pytest
5+
6+
import pandas.util._test_decorators as td
7+
8+
import pandas as pd
9+
from pandas import DataFrame, Series
10+
from pandas.tests.io.pytables.common import ensure_clean_path, ensure_clean_store
11+
import pandas.util.testing as tm
12+
from pandas.util.testing import assert_frame_equal
13+
14+
from pandas.io.pytables import read_hdf
15+
16+
# GH10447
17+
18+
19+
def test_complex_fixed(setup_path):
20+
df = DataFrame(
21+
np.random.rand(4, 5).astype(np.complex64),
22+
index=list("abcd"),
23+
columns=list("ABCDE"),
24+
)
25+
26+
with ensure_clean_path(setup_path) as path:
27+
df.to_hdf(path, "df")
28+
reread = read_hdf(path, "df")
29+
assert_frame_equal(df, reread)
30+
31+
df = DataFrame(
32+
np.random.rand(4, 5).astype(np.complex128),
33+
index=list("abcd"),
34+
columns=list("ABCDE"),
35+
)
36+
with ensure_clean_path(setup_path) as path:
37+
df.to_hdf(path, "df")
38+
reread = read_hdf(path, "df")
39+
assert_frame_equal(df, reread)
40+
41+
42+
def test_complex_table(setup_path):
43+
df = DataFrame(
44+
np.random.rand(4, 5).astype(np.complex64),
45+
index=list("abcd"),
46+
columns=list("ABCDE"),
47+
)
48+
49+
with ensure_clean_path(setup_path) as path:
50+
df.to_hdf(path, "df", format="table")
51+
reread = read_hdf(path, "df")
52+
assert_frame_equal(df, reread)
53+
54+
df = DataFrame(
55+
np.random.rand(4, 5).astype(np.complex128),
56+
index=list("abcd"),
57+
columns=list("ABCDE"),
58+
)
59+
60+
with ensure_clean_path(setup_path) as path:
61+
df.to_hdf(path, "df", format="table", mode="w")
62+
reread = read_hdf(path, "df")
63+
assert_frame_equal(df, reread)
64+
65+
66+
@td.xfail_non_writeable
67+
def test_complex_mixed_fixed(setup_path):
68+
complex64 = np.array(
69+
[1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64
70+
)
71+
complex128 = np.array(
72+
[1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128
73+
)
74+
df = DataFrame(
75+
{
76+
"A": [1, 2, 3, 4],
77+
"B": ["a", "b", "c", "d"],
78+
"C": complex64,
79+
"D": complex128,
80+
"E": [1.0, 2.0, 3.0, 4.0],
81+
},
82+
index=list("abcd"),
83+
)
84+
with ensure_clean_path(setup_path) as path:
85+
df.to_hdf(path, "df")
86+
reread = read_hdf(path, "df")
87+
assert_frame_equal(df, reread)
88+
89+
90+
def test_complex_mixed_table(setup_path):
91+
complex64 = np.array(
92+
[1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64
93+
)
94+
complex128 = np.array(
95+
[1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128
96+
)
97+
df = DataFrame(
98+
{
99+
"A": [1, 2, 3, 4],
100+
"B": ["a", "b", "c", "d"],
101+
"C": complex64,
102+
"D": complex128,
103+
"E": [1.0, 2.0, 3.0, 4.0],
104+
},
105+
index=list("abcd"),
106+
)
107+
108+
with ensure_clean_store(setup_path) as store:
109+
store.append("df", df, data_columns=["A", "B"])
110+
result = store.select("df", where="A>2")
111+
assert_frame_equal(df.loc[df.A > 2], result)
112+
113+
with ensure_clean_path(setup_path) as path:
114+
df.to_hdf(path, "df", format="table")
115+
reread = read_hdf(path, "df")
116+
assert_frame_equal(df, reread)
117+
118+
119+
def test_complex_across_dimensions_fixed(setup_path):
120+
with catch_warnings(record=True):
121+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
122+
s = Series(complex128, index=list("abcd"))
123+
df = DataFrame({"A": s, "B": s})
124+
125+
objs = [s, df]
126+
comps = [tm.assert_series_equal, tm.assert_frame_equal]
127+
for obj, comp in zip(objs, comps):
128+
with ensure_clean_path(setup_path) as path:
129+
obj.to_hdf(path, "obj", format="fixed")
130+
reread = read_hdf(path, "obj")
131+
comp(obj, reread)
132+
133+
134+
def test_complex_across_dimensions(setup_path):
135+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
136+
s = Series(complex128, index=list("abcd"))
137+
df = DataFrame({"A": s, "B": s})
138+
139+
with catch_warnings(record=True):
140+
141+
objs = [df]
142+
comps = [tm.assert_frame_equal]
143+
for obj, comp in zip(objs, comps):
144+
with ensure_clean_path(setup_path) as path:
145+
obj.to_hdf(path, "obj", format="table")
146+
reread = read_hdf(path, "obj")
147+
comp(obj, reread)
148+
149+
150+
def test_complex_indexing_error(setup_path):
151+
complex128 = np.array(
152+
[1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128
153+
)
154+
df = DataFrame(
155+
{"A": [1, 2, 3, 4], "B": ["a", "b", "c", "d"], "C": complex128},
156+
index=list("abcd"),
157+
)
158+
with ensure_clean_store(setup_path) as store:
159+
with pytest.raises(TypeError):
160+
store.append("df", df, data_columns=["C"])
161+
162+
163+
def test_complex_series_error(setup_path):
164+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
165+
s = Series(complex128, index=list("abcd"))
166+
167+
with ensure_clean_path(setup_path) as path:
168+
with pytest.raises(TypeError):
169+
s.to_hdf(path, "obj", format="t")
170+
171+
with ensure_clean_path(setup_path) as path:
172+
s.to_hdf(path, "obj", format="t", index=False)
173+
reread = read_hdf(path, "obj")
174+
tm.assert_series_equal(s, reread)
175+
176+
177+
def test_complex_append(setup_path):
178+
df = DataFrame(
179+
{"a": np.random.randn(100).astype(np.complex128), "b": np.random.randn(100)}
180+
)
181+
182+
with ensure_clean_store(setup_path) as store:
183+
store.append("df", df, data_columns=["b"])
184+
store.append("df", df)
185+
result = store.select("df")
186+
assert_frame_equal(pd.concat([df, df], 0), result)

0 commit comments

Comments
 (0)