Skip to content

Commit 19dc304

Browse files
committed
TST: add TestReadPyTablesHDF5 test scaffold
1 parent 2243629 commit 19dc304

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

pandas/tests/io/test_pytables.py

+84
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from io import BytesIO
66
import os
77
import tempfile
8+
import time
89
from warnings import catch_warnings, simplefilter
910

1011
import numpy as np
@@ -5177,3 +5178,86 @@ def test_dst_transitions(self):
51775178
store.append('df', df)
51785179
result = store.select('df')
51795180
assert_frame_equal(result, df)
5181+
5182+
5183+
class TestReadPyTablesHDF5(Base):
5184+
"""
5185+
A group of tests which covers reading HDF5 files written by plain PyTables
5186+
(not written by pandas).
5187+
"""
5188+
5189+
def _create_simple_hdf5_file_with_pytables(self):
5190+
5191+
table_schema = {
5192+
'c0': tables.Time64Col(pos=0),
5193+
'c1': tables.StringCol(5, pos=1),
5194+
'c2': tables.UInt32Col(pos=2),
5195+
}
5196+
5197+
t0 = time.time()
5198+
5199+
testsamples = [
5200+
{'c0': t0, 'c1': 'aaaaa', 'c2': 1},
5201+
{'c0': t0 + 1, 'c1': 'bbbbb', 'c2': 2},
5202+
{'c0': t0 + 2, 'c1': 'ccccc', 'c2': 10**5},
5203+
{'c0': t0 + 3, 'c1': 'ddddd', 'c2': 4294967295},
5204+
]
5205+
5206+
# This returns a path and does not open the file.
5207+
tmpfilepath = create_tempfile(self.path)
5208+
objectname = 'pandas_test_timeseries'
5209+
5210+
with tables.open_file(tmpfilepath, mode='w') as hf:
5211+
t = hf.create_table('/', name=objectname, description=table_schema)
5212+
for sample in testsamples:
5213+
for key, value in sample.items():
5214+
t.row[key] = value
5215+
t.row.append()
5216+
5217+
return tmpfilepath, objectname, testsamples
5218+
5219+
def _compare(self, df, samples):
5220+
"""Compare the reference `samples` with the contents in DataFrame `df`.
5221+
"""
5222+
for idx, row in df.iterrows():
5223+
# Compare Time64Col values with tolerance.
5224+
tm.assert_almost_equal(samples[idx]['c0'], row['c0'])
5225+
5226+
# Compare a short string.
5227+
assert samples[idx]['c1'] == row['c1']
5228+
5229+
# Compare an unsigned 32 bit integer.
5230+
assert samples[idx]['c2'] == row['c2']
5231+
5232+
def test_read_complete(self):
5233+
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5234+
5235+
df = pd.read_hdf(path, key=objname)
5236+
self._compare(df, samples)
5237+
5238+
def test_read_with_start(self):
5239+
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5240+
5241+
# Currently this fails as of
5242+
# https://github.com/pandas-dev/pandas/issues/11188
5243+
with pytest.raises(ValueError, match='Shape of passed values is'):
5244+
df = pd.read_hdf(path, key=objname, start=1)
5245+
self._compare(df, samples[1:])
5246+
5247+
def test_read_with_stop(self):
5248+
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5249+
5250+
# Currently this fails as of
5251+
# https://github.com/pandas-dev/pandas/issues/11188
5252+
with pytest.raises(ValueError, match='Shape of passed values is'):
5253+
df = pd.read_hdf(path, key=objname, stop=1)
5254+
self._compare(df, samples[0:1])
5255+
5256+
def test_read_with_startstop(self):
5257+
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5258+
5259+
# Currently this fails as of
5260+
# https://github.com/pandas-dev/pandas/issues/11188
5261+
with pytest.raises(ValueError, match='Shape of passed values is'):
5262+
df = pd.read_hdf(path, key=objname, start=1, stop=2)
5263+
self._compare(df, samples[1:2])

0 commit comments

Comments
 (0)