Skip to content

Commit c805c3b

Browse files
toobazjreback
authored andcommitted
ENH: Make HDFStore iterable
closes pandas-dev#12221 HDFStore is not an iterator - but being iterable, it can return an iterator of itself (i.e. of ``.keys()``). Author: Pietro Battiston <[email protected]> Closes pandas-dev#12253 from toobaz/iterable_hdfstore and squashes the following commits: 23bfb3a [Pietro Battiston] ENH: Make HDFStore iterable
1 parent 3073b47 commit c805c3b

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
@@ -395,6 +395,7 @@ Other enhancements
395395
- ``DataFrame.select_dtypes`` now allows the ``np.float16`` typecode (:issue:`11990`)
396396
- ``pivot_table()`` now accepts most iterables for the ``values`` parameter (:issue:`12017`)
397397
- Added Google ``BigQuery`` service account authentication support, which enables authentication on remote servers. (:issue:`11881`). For further details see :ref:`here <io.bigquery_authentication>`
398+
- ``HDFStore`` is now iterable: ``for k in store`` is equivalent to ``for k in store.keys()`` (:issue: `12221`).
398399

399400
.. _whatsnew_0180.api_breaking:
400401

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)