Skip to content

ENH: Simplify using read_hdf for HDF files with one dataset #10443

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

Merged
merged 1 commit into from
Jul 13, 2015
Merged
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.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ New features

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

- ``.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`)

Expand Down
11 changes: 9 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
f(path_or_buf)


def read_hdf(path_or_buf, key, **kwargs):
def read_hdf(path_or_buf, key=None, **kwargs):
""" read from the store, close it if we opened it

Retrieve pandas object stored in file, optionally based on where
Expand All @@ -280,7 +280,8 @@ def read_hdf(path_or_buf, key, **kwargs):
Parameters
----------
path_or_buf : path (string), or buffer to read from
key : group identifier in the store
key : group identifier in the store. Can be omitted a HDF file contains
a single pandas object.
where : list of Term (or convertable) objects, optional
start : optional, integer (defaults to None), row number to start
selection
Expand Down Expand Up @@ -329,6 +330,12 @@ def read_hdf(path_or_buf, key, **kwargs):
'implemented.')

try:
if key is None:
keys = store.keys()
if len(keys) != 1:
raise ValueError('key must be provided when HDF file contains '
'multiple datasets.')
key = keys[0]
return store.select(key, auto_close=auto_close, **kwargs)
except:
# if there is an error, close the store
Expand Down
11 changes: 11 additions & 0 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4731,6 +4731,17 @@ def test_invalid_complib(self):
columns=list('ABCDE'))
with ensure_clean_path(self.path) as path:
self.assertRaises(ValueError, df.to_hdf, path, 'df', complib='blosc:zlib')
# GH10443
def test_read_nokey(self):
df = DataFrame(np.random.rand(4, 5),
index=list('abcd'),
columns=list('ABCDE'))
with ensure_clean_path(self.path) as path:
df.to_hdf(path, 'df', mode='a')
reread = read_hdf(path)
assert_frame_equal(df, reread)
df.to_hdf(path, 'df2', mode='a')
self.assertRaises(ValueError, read_hdf, path)

def _test_sort(obj):
if isinstance(obj, DataFrame):
Expand Down