|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from contextlib import contextmanager |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +from pandas.util.testing import assert_frame_equal |
| 10 | + |
| 11 | + |
| 12 | +tables = pytest.importorskip('tables') |
| 13 | + |
| 14 | + |
| 15 | +def safe_remove(path): |
| 16 | + if path is not None: |
| 17 | + try: |
| 18 | + os.remove(path) |
| 19 | + except OSError: |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +def create_tempfile(path): |
| 24 | + return os.path.join(tempfile.gettempdir(), path) |
| 25 | + |
| 26 | + |
| 27 | +@contextmanager |
| 28 | +def ensure_clean_path(path): |
| 29 | + try: |
| 30 | + if isinstance(path, list): |
| 31 | + filenames = [create_tempfile(p) for p in path] |
| 32 | + yield filenames |
| 33 | + else: |
| 34 | + filenames = [create_tempfile(path)] |
| 35 | + yield filenames[0] |
| 36 | + finally: |
| 37 | + for f in filenames: |
| 38 | + safe_remove(f) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def pytables_hdf5_file(): |
| 43 | + """Use PyTables to create a simple HDF5 file.""" |
| 44 | + |
| 45 | + table_schema = { |
| 46 | + 'c0': tables.Time64Col(pos=0), |
| 47 | + 'c1': tables.StringCol(5, pos=1), |
| 48 | + 'c2': tables.Int64Col(pos=2), |
| 49 | + } |
| 50 | + |
| 51 | + t0 = 1561105000.0 |
| 52 | + |
| 53 | + testsamples = [ |
| 54 | + {'c0': t0, 'c1': 'aaaaa', 'c2': 1}, |
| 55 | + {'c0': t0 + 1, 'c1': 'bbbbb', 'c2': 2}, |
| 56 | + {'c0': t0 + 2, 'c1': 'ccccc', 'c2': 10**5}, |
| 57 | + {'c0': t0 + 3, 'c1': 'ddddd', 'c2': 4294967295}, |
| 58 | + ] |
| 59 | + |
| 60 | + objname = 'pandas_test_timeseries' |
| 61 | + |
| 62 | + with ensure_clean_path('pytables_hdf5_file') as path: |
| 63 | + # The `ensure_clean_path` context mgr removes the temp file upon exit. |
| 64 | + with tables.open_file(path, mode='w') as f: |
| 65 | + t = f.create_table('/', name=objname, description=table_schema) |
| 66 | + for sample in testsamples: |
| 67 | + for key, value in sample.items(): |
| 68 | + t.row[key] = value |
| 69 | + t.row.append() |
| 70 | + |
| 71 | + yield path, objname, pd.DataFrame(testsamples) |
| 72 | + |
| 73 | + |
| 74 | +class TestReadPyTablesHDF5: |
| 75 | + """ |
| 76 | + A group of tests which covers reading HDF5 files written by plain PyTables |
| 77 | + (not written by pandas). |
| 78 | + """ |
| 79 | + |
| 80 | + def test_read_complete(self, pytables_hdf5_file): |
| 81 | + path, objname, df = pytables_hdf5_file |
| 82 | + result = pd.read_hdf(path, key=objname) |
| 83 | + expected = df |
| 84 | + assert_frame_equal(result, expected) |
| 85 | + |
| 86 | + def test_read_with_start(self, pytables_hdf5_file): |
| 87 | + path, objname, df = pytables_hdf5_file |
| 88 | + # This is a regression test for pandas-dev/pandas/issues/11188 |
| 89 | + result = pd.read_hdf(path, key=objname, start=1) |
| 90 | + expected = df[1:].reset_index(drop=True) |
| 91 | + assert_frame_equal(result, expected) |
| 92 | + |
| 93 | + def test_read_with_stop(self, pytables_hdf5_file): |
| 94 | + path, objname, df = pytables_hdf5_file |
| 95 | + # This is a regression test for pandas-dev/pandas/issues/11188 |
| 96 | + result = pd.read_hdf(path, key=objname, stop=1) |
| 97 | + expected = df[:1].reset_index(drop=True) |
| 98 | + assert_frame_equal(result, expected) |
| 99 | + |
| 100 | + def test_read_with_startstop(self, pytables_hdf5_file): |
| 101 | + path, objname, df = pytables_hdf5_file |
| 102 | + # This is a regression test for pandas-dev/pandas/issues/11188 |
| 103 | + result = pd.read_hdf(path, key=objname, start=1, stop=2) |
| 104 | + expected = df[1:2].reset_index(drop=True) |
| 105 | + assert_frame_equal(result, expected) |
0 commit comments