Skip to content

Commit 23bfb3a

Browse files
committed
ENH: Make HDFStore iterable
closes #12221
1 parent fe201a2 commit 23bfb3a

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Other enhancements
246246
- ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`)
247247
- ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`)
248248
- Added Google ``BigQuery`` service account authentication support, which enables authentication on remote servers. (:issue:`11881`). For further details see :ref:`here <io.bigquery_authentication>`
249+
- ``HDFStore`` is now iterable: ``for k in store`` is equivalent to ``for k in store.keys()`` (:issue: `12221`).
249250

250251
.. _whatsnew_0180.api_breaking:
251252

pandas/io/pytables.py

+3
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ def keys(self):
488488
"""
489489
return [n._v_pathname for n in self.groups()]
490490

491+
def __iter__(self):
492+
return iter(self.keys())
493+
491494
def items(self):
492495
"""
493496
iterate on key->group

pandas/io/tests/test_pytables.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,15 @@ def test_keys(self):
389389
store['d'] = tm.makePanel()
390390
store['foo/bar'] = tm.makePanel()
391391
self.assertEqual(len(store), 5)
392-
self.assertTrue(set(
393-
store.keys()) == set(['/a', '/b', '/c', '/d', '/foo/bar']))
392+
expected = set(['/a', '/b', '/c', '/d', '/foo/bar'])
393+
self.assertTrue(set(store.keys()) == expected)
394+
self.assertTrue(set(store) == expected)
395+
396+
def test_iter_empty(self):
397+
398+
with ensure_clean_path(self.path) as path:
399+
# GH 12221
400+
self.assertTrue(list(pd.HDFStore(path)) == [])
394401

395402
def test_repr(self):
396403

0 commit comments

Comments
 (0)