Skip to content

Commit 0baf2ba

Browse files
committed
TST: add TestReadPyTablesHDF5 test scaffold
1 parent 77e6556 commit 0baf2ba

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

0 commit comments

Comments
 (0)