Skip to content

Commit 9d49b5c

Browse files
author
Vraj Mohan
committed
TST: Split out test_pytables.py to sub-module of tests
Fixes pandas-dev#18498 Split test_pytables.py into: - pytables/base.py - pytables/test_complex_values.py - pytables/test_pytables.py - pytables/test_timezones.py
1 parent 6449bc1 commit 9d49b5c

File tree

5 files changed

+659
-624
lines changed

5 files changed

+659
-624
lines changed

pandas/tests/io/pytables/__init__.py

Whitespace-only changes.

pandas/tests/io/pytables/base.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from contextlib import contextmanager
2+
import os
3+
import tempfile
4+
5+
import pandas.util.testing as tm
6+
7+
from pandas.io.pytables import HDFStore
8+
9+
10+
class Base(object):
11+
12+
@classmethod
13+
def setup_class(cls):
14+
15+
# Pytables 3.0.0 deprecates lots of things
16+
tm.reset_testing_mode()
17+
18+
@classmethod
19+
def teardown_class(cls):
20+
21+
# Pytables 3.0.0 deprecates lots of things
22+
tm.set_testing_mode()
23+
24+
def setup_method(self, method):
25+
self.path = 'tmp.__%s__.h5' % tm.rands(10)
26+
27+
def teardown_method(self, method):
28+
pass
29+
30+
31+
def safe_close(store):
32+
try:
33+
if store is not None:
34+
store.close()
35+
except IOError:
36+
pass
37+
38+
39+
@contextmanager
40+
def ensure_clean_store(path, mode='a', complevel=None, complib=None,
41+
fletcher32=False):
42+
43+
store = None
44+
try:
45+
46+
# put in the temporary path if we don't have one already
47+
if not len(os.path.dirname(path)):
48+
path = create_tempfile(path)
49+
50+
store = HDFStore(path, mode=mode, complevel=complevel,
51+
complib=complib, fletcher32=False)
52+
yield store
53+
finally:
54+
safe_close(store)
55+
if mode == 'w' or mode == 'a':
56+
safe_remove(path)
57+
58+
59+
@contextmanager
60+
def ensure_clean_path(path):
61+
"""
62+
return essentially a named temporary file that is not opened
63+
and deleted on existing; if path is a list, then create and
64+
return list of filenames
65+
"""
66+
filenames = []
67+
try:
68+
if isinstance(path, list):
69+
filenames = [create_tempfile(p) for p in path]
70+
yield filenames
71+
else:
72+
filenames = [create_tempfile(path)]
73+
yield filenames[0]
74+
finally:
75+
for f in filenames:
76+
safe_remove(f)
77+
78+
79+
def safe_remove(path):
80+
if path is not None:
81+
try:
82+
os.remove(path)
83+
except OSError:
84+
pass
85+
86+
87+
def create_tempfile(path):
88+
""" create an unopened named temporary file """
89+
return os.path.join(tempfile.gettempdir(), path)
90+
91+
92+
def maybe_remove(store, key):
93+
"""For tests using tables, try removing the table to be sure there is
94+
no content from previous tests using the same table name."""
95+
try:
96+
store.remove(key)
97+
except (ValueError, KeyError):
98+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
from warnings import catch_warnings
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas import DataFrame, Panel, Series, concat
7+
from pandas.util.testing import (
8+
assert_frame_equal, assert_panel_equal, assert_series_equal)
9+
10+
from pandas.io.pytables import read_hdf
11+
12+
from .base import Base, ensure_clean_path, ensure_clean_store
13+
14+
15+
class TestHDFComplexValues(Base):
16+
# GH10447
17+
18+
def test_complex_fixed(self):
19+
df = DataFrame(np.random.rand(4, 5).astype(np.complex64),
20+
index=list('abcd'),
21+
columns=list('ABCDE'))
22+
23+
with ensure_clean_path(self.path) as path:
24+
df.to_hdf(path, 'df')
25+
reread = read_hdf(path, 'df')
26+
assert_frame_equal(df, reread)
27+
28+
df = DataFrame(np.random.rand(4, 5).astype(np.complex128),
29+
index=list('abcd'),
30+
columns=list('ABCDE'))
31+
with ensure_clean_path(self.path) as path:
32+
df.to_hdf(path, 'df')
33+
reread = read_hdf(path, 'df')
34+
assert_frame_equal(df, reread)
35+
36+
def test_complex_table(self):
37+
df = DataFrame(np.random.rand(4, 5).astype(np.complex64),
38+
index=list('abcd'),
39+
columns=list('ABCDE'))
40+
41+
with ensure_clean_path(self.path) as path:
42+
df.to_hdf(path, 'df', format='table')
43+
reread = read_hdf(path, 'df')
44+
assert_frame_equal(df, reread)
45+
46+
df = DataFrame(np.random.rand(4, 5).astype(np.complex128),
47+
index=list('abcd'),
48+
columns=list('ABCDE'))
49+
50+
with ensure_clean_path(self.path) as path:
51+
df.to_hdf(path, 'df', format='table', mode='w')
52+
reread = read_hdf(path, 'df')
53+
assert_frame_equal(df, reread)
54+
55+
def test_complex_mixed_fixed(self):
56+
complex64 = np.array([1.0 + 1.0j, 1.0 + 1.0j,
57+
1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64)
58+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j],
59+
dtype=np.complex128)
60+
df = DataFrame({'A': [1, 2, 3, 4],
61+
'B': ['a', 'b', 'c', 'd'],
62+
'C': complex64,
63+
'D': complex128,
64+
'E': [1.0, 2.0, 3.0, 4.0]},
65+
index=list('abcd'))
66+
with ensure_clean_path(self.path) as path:
67+
df.to_hdf(path, 'df')
68+
reread = read_hdf(path, 'df')
69+
assert_frame_equal(df, reread)
70+
71+
def test_complex_mixed_table(self):
72+
complex64 = np.array([1.0 + 1.0j, 1.0 + 1.0j,
73+
1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64)
74+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j],
75+
dtype=np.complex128)
76+
df = DataFrame({'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+
index=list('abcd'))
82+
83+
with ensure_clean_store(self.path) as store:
84+
store.append('df', df, data_columns=['A', 'B'])
85+
result = store.select('df', where='A>2')
86+
assert_frame_equal(df.loc[df.A > 2], result)
87+
88+
with ensure_clean_path(self.path) as path:
89+
df.to_hdf(path, 'df', format='table')
90+
reread = read_hdf(path, 'df')
91+
assert_frame_equal(df, reread)
92+
93+
@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning")
94+
def test_complex_across_dimensions_fixed(self):
95+
with catch_warnings(record=True):
96+
complex128 = np.array(
97+
[1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
98+
s = Series(complex128, index=list('abcd'))
99+
df = DataFrame({'A': s, 'B': s})
100+
p = Panel({'One': df, 'Two': df})
101+
102+
objs = [s, df, p]
103+
comps = [assert_series_equal, assert_frame_equal,
104+
assert_panel_equal]
105+
for obj, comp in zip(objs, comps):
106+
with ensure_clean_path(self.path) as path:
107+
obj.to_hdf(path, 'obj', format='fixed')
108+
reread = read_hdf(path, 'obj')
109+
comp(obj, reread)
110+
111+
@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning")
112+
def test_complex_across_dimensions(self):
113+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
114+
s = Series(complex128, index=list('abcd'))
115+
df = DataFrame({'A': s, 'B': s})
116+
117+
with catch_warnings(record=True):
118+
p = Panel({'One': df, 'Two': df})
119+
120+
objs = [df, p]
121+
comps = [assert_frame_equal, assert_panel_equal]
122+
for obj, comp in zip(objs, comps):
123+
with ensure_clean_path(self.path) as path:
124+
obj.to_hdf(path, 'obj', format='table')
125+
reread = read_hdf(path, 'obj')
126+
comp(obj, reread)
127+
128+
def test_complex_indexing_error(self):
129+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j],
130+
dtype=np.complex128)
131+
df = DataFrame({'A': [1, 2, 3, 4],
132+
'B': ['a', 'b', 'c', 'd'],
133+
'C': complex128},
134+
index=list('abcd'))
135+
with ensure_clean_store(self.path) as store:
136+
pytest.raises(TypeError, store.append,
137+
'df', df, data_columns=['C'])
138+
139+
def test_complex_series_error(self):
140+
complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
141+
s = Series(complex128, index=list('abcd'))
142+
143+
with ensure_clean_path(self.path) as path:
144+
pytest.raises(TypeError, s.to_hdf, path, 'obj', format='t')
145+
146+
with ensure_clean_path(self.path) as path:
147+
s.to_hdf(path, 'obj', format='t', index=False)
148+
reread = read_hdf(path, 'obj')
149+
assert_series_equal(s, reread)
150+
151+
def test_complex_append(self):
152+
df = DataFrame({'a': np.random.randn(100).astype(np.complex128),
153+
'b': np.random.randn(100)})
154+
155+
with ensure_clean_store(self.path) as store:
156+
store.append('df', df, data_columns=['b'])
157+
store.append('df', df)
158+
result = store.select('df')
159+
assert_frame_equal(concat([df, df], 0), result)

0 commit comments

Comments
 (0)