Skip to content

Commit 53dba1a

Browse files
committed
TST: move tests to io/pytables/test_compat.py
1 parent aed78ff commit 53dba1a

File tree

2 files changed

+105
-67
lines changed

2 files changed

+105
-67
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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)

pandas/tests/io/test_pytables.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5177,70 +5177,3 @@ def test_dst_transitions(self):
51775177
store.append('df', df)
51785178
result = store.select('df')
51795179
assert_frame_equal(result, df)
5180-
5181-
5182-
@pytest.fixture
5183-
def pytables_hdf5_file():
5184-
"""Use PyTables to create a simple HDF5 file."""
5185-
5186-
table_schema = {
5187-
'c0': tables.Time64Col(pos=0),
5188-
'c1': tables.StringCol(5, pos=1),
5189-
'c2': tables.Int64Col(pos=2),
5190-
}
5191-
5192-
t0 = 1561105000.0
5193-
5194-
testsamples = [
5195-
{'c0': t0, 'c1': 'aaaaa', 'c2': 1},
5196-
{'c0': t0 + 1, 'c1': 'bbbbb', 'c2': 2},
5197-
{'c0': t0 + 2, 'c1': 'ccccc', 'c2': 10**5},
5198-
{'c0': t0 + 3, 'c1': 'ddddd', 'c2': 4294967295},
5199-
]
5200-
5201-
objname = 'pandas_test_timeseries'
5202-
5203-
with ensure_clean_path('pytables_hdf5_file') as path:
5204-
# The `ensure_clean_path` context mgr removes the temp file upon exit.
5205-
with tables.open_file(path, mode='w') as f:
5206-
t = f.create_table('/', name=objname, description=table_schema)
5207-
for sample in testsamples:
5208-
for key, value in sample.items():
5209-
t.row[key] = value
5210-
t.row.append()
5211-
5212-
yield path, objname, pd.DataFrame(testsamples)
5213-
5214-
5215-
class TestReadPyTablesHDF5:
5216-
"""
5217-
A group of tests which covers reading HDF5 files written by plain PyTables
5218-
(not written by pandas).
5219-
"""
5220-
5221-
def test_read_complete(self, pytables_hdf5_file):
5222-
path, objname, df = pytables_hdf5_file
5223-
result = pd.read_hdf(path, key=objname)
5224-
expected = df
5225-
assert_frame_equal(result, expected)
5226-
5227-
def test_read_with_start(self, pytables_hdf5_file):
5228-
path, objname, df = pytables_hdf5_file
5229-
# This is a regression test for pandas-dev/pandas/issues/11188
5230-
result = pd.read_hdf(path, key=objname, start=1)
5231-
expected = df[1:].reset_index(drop=True)
5232-
assert_frame_equal(result, expected)
5233-
5234-
def test_read_with_stop(self, pytables_hdf5_file):
5235-
path, objname, df = pytables_hdf5_file
5236-
# This is a regression test for pandas-dev/pandas/issues/11188
5237-
result = pd.read_hdf(path, key=objname, stop=1)
5238-
expected = df[:1].reset_index(drop=True)
5239-
assert_frame_equal(result, expected)
5240-
5241-
def test_read_with_startstop(self, pytables_hdf5_file):
5242-
path, objname, df = pytables_hdf5_file
5243-
# This is a regression test for pandas-dev/pandas/issues/11188
5244-
result = pd.read_hdf(path, key=objname, start=1, stop=2)
5245-
expected = df[1:2].reset_index(drop=True)
5246-
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)