diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 421822380c2da..2216b88769053 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -246,6 +246,7 @@ Other enhancements - ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`) - ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`) - Added Google ``BigQuery`` service account authentication support, which enables authentication on remote servers. (:issue:`11881`). For further details see :ref:`here ` +- ``HDFStore`` is now iterable: ``for k in store`` is equivalent to ``for k in store.keys()`` (:issue: `12221`). .. _whatsnew_0180.api_breaking: diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index cd8f7699a85d1..c94b387f7554a 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -488,6 +488,9 @@ def keys(self): """ return [n._v_pathname for n in self.groups()] + def __iter__(self): + return iter(self.keys()) + def items(self): """ iterate on key->group diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py index b08d24747bcd3..192ed4e687272 100644 --- a/pandas/io/tests/test_pytables.py +++ b/pandas/io/tests/test_pytables.py @@ -389,8 +389,15 @@ def test_keys(self): store['d'] = tm.makePanel() store['foo/bar'] = tm.makePanel() self.assertEqual(len(store), 5) - self.assertTrue(set( - store.keys()) == set(['/a', '/b', '/c', '/d', '/foo/bar'])) + expected = set(['/a', '/b', '/c', '/d', '/foo/bar']) + self.assertTrue(set(store.keys()) == expected) + self.assertTrue(set(store) == expected) + + def test_iter_empty(self): + + with ensure_clean_path(self.path) as path: + # GH 12221 + self.assertTrue(list(pd.HDFStore(path)) == []) def test_repr(self):