From 7b1e9c50c3438a1e9dee1351a5fe3ae1748c0c95 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 1 Jun 2017 13:31:29 -0500 Subject: [PATCH] TST: Make HDF5 fspath write test robust 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. --- pandas/tests/io/test_common.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index 289f86eb2dc53..b527e3c5dc254 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -143,7 +143,6 @@ def test_read_fspath_all(self, reader, module, path): ('to_csv', {}, 'os'), ('to_excel', {'engine': 'xlwt'}, 'xlwt'), ('to_feather', {}, 'feather'), - ('to_hdf', {'key': 'bar', 'mode': 'w'}, 'tables'), ('to_html', {}, 'os'), ('to_json', {}, 'os'), ('to_latex', {}, 'os'), @@ -171,6 +170,26 @@ def test_write_fspath_all(self, writer_name, writer_kwargs, module): assert result == expected + def test_write_fspath_hdf5(self): + # Same test as write_fspath_all, except HDF5 files aren't + # necessarily byte-for-byte identical for a given dataframe, so we'll + # have to read and compare equality + pytest.importorskip('tables') + + df = pd.DataFrame({"A": [1, 2]}) + p1 = tm.ensure_clean('string') + p2 = tm.ensure_clean('fspath') + + with p1 as string, p2 as fspath: + mypath = CustomFSPath(fspath) + df.to_hdf(mypath, key='bar') + df.to_hdf(string, key='bar') + + result = pd.read_hdf(fspath, key='bar') + expected = pd.read_hdf(string, key='bar') + + tm.assert_frame_equal(result, expected) + class TestMMapWrapper(object):