Skip to content

Commit b0ae071

Browse files
Stephen PascoeStephen Pascoe
Stephen Pascoe
authored and
Stephen Pascoe
committed
Documentation and whats-new.
Including small fix to remove redundant '/' from group names.
1 parent 18f2d48 commit b0ae071

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

doc/source/io.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,19 @@ everything in the sub-store and BELOW, so be *careful*.
26772677
store.remove('food')
26782678
store
26792679
2680+
You can walk through the group hierarchy using the ``walk`` method which
2681+
will yield a tuple for each group key along with the relative keys of its contents.
2682+
2683+
.. ipython:: python
2684+
2685+
for (path, subgroups, subkeys) in store.walk():
2686+
for subgroup in subgroups:
2687+
print('GROUP: {}/{}'.format(path, subgroup))
2688+
for subkey in subkeys:
2689+
key = '/'.join([path, subkey])
2690+
print('KEY: {}'.format(key))
2691+
print(store.get(key))
2692+
26802693
.. _io.hdf5-types:
26812694

26822695
Storing Mixed Types in a Table

doc/source/whatsnew/v0.17.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ Other enhancements
255255

256256
pd.concat([foo, bar, baz], 1)
257257

258+
- New method ``HDFStore.walk`` will recursively walk the group hierarchy of a HDF5 file (:issue:`10932`)
259+
258260
.. _whatsnew_0170.api:
259261

260262
.. _whatsnew_0170.api_breaking:

pandas/io/pytables.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def walk(self):
10441044
10451045
Returns
10461046
-------
1047-
A generator yielding tuples (`path`, `groups`, `frames`) where:
1047+
A generator yielding tuples (`path`, `groups`, `leaves`) where:
10481048
10491049
- `path` is the full path to a group,
10501050
- `groups` is a list of group names contained in `path`
@@ -1066,7 +1066,8 @@ def walk(self):
10661066
groups.append(child._v_name)
10671067
else:
10681068
leaves.append(child._v_name)
1069-
yield (g._v_pathname, groups, leaves)
1069+
1070+
yield (g._v_pathname.rstrip('/'), groups, leaves)
10701071

10711072

10721073
def get_node(self, key):

pandas/io/tests/test_pytables.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4825,9 +4825,7 @@ def test_walk(self):
48254825
'a1': np.array([[1,2,3], [4,5,6]])
48264826
}
48274827

4828-
with tm.ensure_clean('walk_groups.hdf') as filename:
4829-
store = HDFStore(filename, 'w')
4830-
4828+
with ensure_clean_store('walk_groups.hdf', mode='w') as store:
48314829
store.put('/first_group/df1', objs['df1'])
48324830
store.put('/first_group/df2', objs['df2'])
48334831
store.put('/second_group/df3', objs['df3'])
@@ -4837,7 +4835,7 @@ def test_walk(self):
48374835
store._handle.create_array(g1, 'a1', objs['a1'])
48384836

48394837
expect = {
4840-
'/': (set(['first_group', 'second_group']), set()),
4838+
'': (set(['first_group', 'second_group']), set()),
48414839
'/first_group': (set(), set(['df1', 'df2'])),
48424840
'/second_group': (set(['third_group']), set(['df3', 's1'])),
48434841
'/second_group/third_group': (set(), set(['df4'])),

0 commit comments

Comments
 (0)