Skip to content

ENH: Make HDFStore iterable #12253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <io.bigquery_authentication>`
- ``HDFStore`` is now iterable: ``for k in store`` is equivalent to ``for k in store.keys()`` (:issue: `12221`).

.. _whatsnew_0180.api_breaking:

Expand Down
3 changes: 3 additions & 0 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add the tests from the original issue exactly (as a new test)

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):

Expand Down