Skip to content

Commit 83b2320

Browse files
committed
Merge pull request #10443 from bashtage/read-hdf-singleton
ENH: Simplify using read_hdf for HDF files with one dataset
2 parents df1f5cf + eccbfa7 commit 83b2320

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ New features
3232

3333
Other enhancements
3434
^^^^^^^^^^^^^^^^^^
35+
- Enable `read_hdf` to be used without specifying a key when the HDF file contains a single dataset (:issue:`10443`)
3536

3637
- ``.as_blocks`` will now take a ``copy`` optional argument to return a copy of the data, default is to copy (no change in behavior from prior versions), (:issue:`9607`)
3738

pandas/io/pytables.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
271271
f(path_or_buf)
272272

273273

274-
def read_hdf(path_or_buf, key, **kwargs):
274+
def read_hdf(path_or_buf, key=None, **kwargs):
275275
""" read from the store, close it if we opened it
276276
277277
Retrieve pandas object stored in file, optionally based on where
@@ -280,7 +280,8 @@ def read_hdf(path_or_buf, key, **kwargs):
280280
Parameters
281281
----------
282282
path_or_buf : path (string), or buffer to read from
283-
key : group identifier in the store
283+
key : group identifier in the store. Can be omitted a HDF file contains
284+
a single pandas object.
284285
where : list of Term (or convertable) objects, optional
285286
start : optional, integer (defaults to None), row number to start
286287
selection
@@ -329,6 +330,12 @@ def read_hdf(path_or_buf, key, **kwargs):
329330
'implemented.')
330331

331332
try:
333+
if key is None:
334+
keys = store.keys()
335+
if len(keys) != 1:
336+
raise ValueError('key must be provided when HDF file contains '
337+
'multiple datasets.')
338+
key = keys[0]
332339
return store.select(key, auto_close=auto_close, **kwargs)
333340
except:
334341
# if there is an error, close the store

pandas/io/tests/test_pytables.py

+11
Original file line numberDiff line numberDiff line change
@@ -4731,6 +4731,17 @@ def test_invalid_complib(self):
47314731
columns=list('ABCDE'))
47324732
with ensure_clean_path(self.path) as path:
47334733
self.assertRaises(ValueError, df.to_hdf, path, 'df', complib='blosc:zlib')
4734+
# GH10443
4735+
def test_read_nokey(self):
4736+
df = DataFrame(np.random.rand(4, 5),
4737+
index=list('abcd'),
4738+
columns=list('ABCDE'))
4739+
with ensure_clean_path(self.path) as path:
4740+
df.to_hdf(path, 'df', mode='a')
4741+
reread = read_hdf(path)
4742+
assert_frame_equal(df, reread)
4743+
df.to_hdf(path, 'df2', mode='a')
4744+
self.assertRaises(ValueError, read_hdf, path)
47344745

47354746
def _test_sort(obj):
47364747
if isinstance(obj, DataFrame):

0 commit comments

Comments
 (0)