Skip to content

Commit 50479ae

Browse files
TomAugspurgerKiv
authored andcommitted
TST: Make HDF5 fspath write test robust (pandas-dev#16575)
The test_write_fspath_all test would fail on the HDF5 example occasionally (about 1/100 in my experience). Apparently you don't get an identical HDF5 every single time. This refactors that test out to its own where we write and read both versions, and compare equality that way.
1 parent a7c95f2 commit 50479ae

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

pandas/tests/io/test_common.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ def test_read_fspath_all(self, reader, module, path):
143143
('to_csv', {}, 'os'),
144144
('to_excel', {'engine': 'xlwt'}, 'xlwt'),
145145
('to_feather', {}, 'feather'),
146-
('to_hdf', {'key': 'bar', 'mode': 'w'}, 'tables'),
147146
('to_html', {}, 'os'),
148147
('to_json', {}, 'os'),
149148
('to_latex', {}, 'os'),
@@ -171,6 +170,26 @@ def test_write_fspath_all(self, writer_name, writer_kwargs, module):
171170

172171
assert result == expected
173172

173+
def test_write_fspath_hdf5(self):
174+
# Same test as write_fspath_all, except HDF5 files aren't
175+
# necessarily byte-for-byte identical for a given dataframe, so we'll
176+
# have to read and compare equality
177+
pytest.importorskip('tables')
178+
179+
df = pd.DataFrame({"A": [1, 2]})
180+
p1 = tm.ensure_clean('string')
181+
p2 = tm.ensure_clean('fspath')
182+
183+
with p1 as string, p2 as fspath:
184+
mypath = CustomFSPath(fspath)
185+
df.to_hdf(mypath, key='bar')
186+
df.to_hdf(string, key='bar')
187+
188+
result = pd.read_hdf(fspath, key='bar')
189+
expected = pd.read_hdf(string, key='bar')
190+
191+
tm.assert_frame_equal(result, expected)
192+
174193

175194
class TestMMapWrapper(object):
176195

0 commit comments

Comments
 (0)