test_compat.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import pytest
  2. import pandas as pd
  3. import pandas._testing as tm
  4. tables = pytest.importorskip("tables")
  5. @pytest.fixture
  6. def pytables_hdf5_file(tmp_path):
  7. """
  8. Use PyTables to create a simple HDF5 file.
  9. """
  10. table_schema = {
  11. "c0": tables.Time64Col(pos=0),
  12. "c1": tables.StringCol(5, pos=1),
  13. "c2": tables.Int64Col(pos=2),
  14. }
  15. t0 = 1_561_105_000.0
  16. testsamples = [
  17. {"c0": t0, "c1": "aaaaa", "c2": 1},
  18. {"c0": t0 + 1, "c1": "bbbbb", "c2": 2},
  19. {"c0": t0 + 2, "c1": "ccccc", "c2": 10**5},
  20. {"c0": t0 + 3, "c1": "ddddd", "c2": 4_294_967_295},
  21. ]
  22. objname = "pandas_test_timeseries"
  23. path = tmp_path / "written_with_pytables.h5"
  24. with tables.open_file(path, mode="w") as f:
  25. t = f.create_table("/", name=objname, description=table_schema)
  26. for sample in testsamples:
  27. for key, value in sample.items():
  28. t.row[key] = value
  29. t.row.append()
  30. yield path, objname, pd.DataFrame(testsamples)
  31. class TestReadPyTablesHDF5:
  32. """
  33. A group of tests which covers reading HDF5 files written by plain PyTables
  34. (not written by pandas).
  35. Was introduced for regression-testing issue 11188.
  36. """
  37. def test_read_complete(self, pytables_hdf5_file):
  38. path, objname, df = pytables_hdf5_file
  39. result = pd.read_hdf(path, key=objname)
  40. expected = df
  41. tm.assert_frame_equal(result, expected, check_index_type=True)
  42. def test_read_with_start(self, pytables_hdf5_file):
  43. path, objname, df = pytables_hdf5_file
  44. # This is a regression test for pandas-dev/pandas/issues/11188
  45. result = pd.read_hdf(path, key=objname, start=1)
  46. expected = df[1:].reset_index(drop=True)
  47. tm.assert_frame_equal(result, expected, check_index_type=True)
  48. def test_read_with_stop(self, pytables_hdf5_file):
  49. path, objname, df = pytables_hdf5_file
  50. # This is a regression test for pandas-dev/pandas/issues/11188
  51. result = pd.read_hdf(path, key=objname, stop=1)
  52. expected = df[:1].reset_index(drop=True)
  53. tm.assert_frame_equal(result, expected, check_index_type=True)
  54. def test_read_with_startstop(self, pytables_hdf5_file):
  55. path, objname, df = pytables_hdf5_file
  56. # This is a regression test for pandas-dev/pandas/issues/11188
  57. result = pd.read_hdf(path, key=objname, start=1, stop=2)
  58. expected = df[1:2].reset_index(drop=True)
  59. tm.assert_frame_equal(result, expected, check_index_type=True)