|
5 | 5 | from io import BytesIO
|
6 | 6 | import os
|
7 | 7 | import tempfile
|
| 8 | +import time |
8 | 9 | from warnings import catch_warnings, simplefilter
|
9 | 10 |
|
10 | 11 | import numpy as np
|
@@ -5167,3 +5168,75 @@ def test_dst_transitions(self):
|
5167 | 5168 | store.append('df', df)
|
5168 | 5169 | result = store.select('df')
|
5169 | 5170 | 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': 10**5}, |
| 5192 | + {'c0': t0 + 2, 'c1': 'ccccc', 'c2': 4294967295}, |
| 5193 | + ] |
| 5194 | + |
| 5195 | + # This returns a path and does not open the file. |
| 5196 | + tmpfilepath = create_tempfile(self.path) |
| 5197 | + object_name = 'pandas_test_timeseries' |
| 5198 | + |
| 5199 | + with tables.open_file(tmpfilepath, mode='w') as hdf5file: |
| 5200 | + hdf5table = hdf5file.create_table( |
| 5201 | + where='/', |
| 5202 | + name=object_name, |
| 5203 | + description=table_schema |
| 5204 | + ) |
| 5205 | + |
| 5206 | + for sample in testsamples: |
| 5207 | + for key, value in sample.items(): |
| 5208 | + hdf5table.row[key] = value |
| 5209 | + hdf5table.row.append() |
| 5210 | + |
| 5211 | + return tmpfilepath, object_name, testsamples |
| 5212 | + |
| 5213 | + def test_read_complete(self): |
| 5214 | + path, objname, samples = self._create_simple_hdf5_file_with_pytables() |
| 5215 | + |
| 5216 | + df = pd.read_hdf(path, key=objname) |
| 5217 | + |
| 5218 | + for index, row in df.iterrows(): |
| 5219 | + # Compare Time64Col values with tolerance. |
| 5220 | + tm.assert_almost_equal(samples[index]['c0'], row['c0']) |
| 5221 | + |
| 5222 | + # Compare a short string. |
| 5223 | + assert samples[index]['c1'] == row['c1'] |
| 5224 | + |
| 5225 | + # Compare an unsigned 32 bit integer. |
| 5226 | + assert samples[index]['c2'] == row['c2'] |
| 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 | + |
| 5236 | + def test_read_with_stop(self): |
| 5237 | + path, objname, samples = self._create_simple_hdf5_file_with_pytables() |
| 5238 | + |
| 5239 | + # Currently this fails as of |
| 5240 | + # https://github.com/pandas-dev/pandas/issues/11188 |
| 5241 | + with pytest.raises(ValueError, match='Shape of passed values is'): |
| 5242 | + df = pd.read_hdf(path, key=objname, stop=1) |
0 commit comments