Skip to content

Commit 51047d4

Browse files
Kevin Sheppardjreback
Kevin Sheppard
authored andcommitted
BUG: Remove auto_close option form read_hdf, #9327, #10330
Remove docstring indicating auto_close can be used in read_hdf. This value is always ignored. Also correctly implements support for passing open HDFStores. Adds error handling for unknown buffer-types. xref #9327
1 parent 06b9f6e commit 51047d4

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

doc/source/whatsnew/v0.16.2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,6 @@ Bug Fixes
171171
- Bug in ``read_csv`` with a ``date_parser`` that returned a ``datetime64`` array of other time resolution than ``[ns]`` (:issue:`10245`)
172172

173173
- Bug in ``Panel.apply`` when the result has ndim = 0 (:issue:`10332`)
174+
175+
- Bug in ``read_hdf`` where ``auto_close`` could not be passed (:issue:`9327`).
176+
- Bug in ``read_hdf`` where open stores could not be used (:issue:`10330`).

pandas/io/pytables.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,6 @@ def read_hdf(path_or_buf, key, **kwargs):
290290
return columns
291291
iterator : optional, boolean, return an iterator, default False
292292
chunksize : optional, nrows to include in iteration, return an iterator
293-
auto_close : optional, boolean, should automatically close the store
294-
when finished, default is False
295293
296294
Returns
297295
-------
@@ -303,9 +301,6 @@ def read_hdf(path_or_buf, key, **kwargs):
303301
if 'where' in kwargs:
304302
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)
305303

306-
f = lambda store, auto_close: store.select(
307-
key, auto_close=auto_close, **kwargs)
308-
309304
if isinstance(path_or_buf, string_types):
310305

311306
try:
@@ -321,20 +316,28 @@ def read_hdf(path_or_buf, key, **kwargs):
321316
# can't auto open/close if we are using an iterator
322317
# so delegate to the iterator
323318
store = HDFStore(path_or_buf, **kwargs)
324-
try:
325-
return f(store, True)
326-
except:
319+
auto_close = True
327320

328-
# if there is an error, close the store
329-
try:
330-
store.close()
331-
except:
332-
pass
321+
elif isinstance(path_or_buf, HDFStore):
322+
if not path_or_buf.is_open:
323+
raise IOError('The HDFStore must be open for reading.')
333324

334-
raise
325+
store = path_or_buf
326+
auto_close = False
327+
else:
328+
raise NotImplementedError('Support for generic buffers has not been '
329+
'implemented.')
330+
331+
try:
332+
return store.select(key, auto_close=auto_close, **kwargs)
333+
except:
334+
# if there is an error, close the store
335+
try:
336+
store.close()
337+
except:
338+
pass
335339

336-
# a passed store; user controls open/close
337-
f(path_or_buf, False)
340+
raise
338341

339342

340343
class HDFStore(StringMixin):

pandas/io/tests/test_pytables.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pandas import (Series, DataFrame, Panel, MultiIndex, Categorical, bdate_range,
1414
date_range, timedelta_range, Index, DatetimeIndex, TimedeltaIndex, isnull)
1515

16-
from pandas.io.pytables import _tables
16+
from pandas.io.pytables import _tables, TableIterator
1717
try:
1818
_tables()
1919
except ImportError as e:
@@ -4670,6 +4670,54 @@ def test_to_hdf_with_object_column_names(self):
46704670
assert(len(result))
46714671

46724672

4673+
def test_read_hdf_open_store(self):
4674+
# GH10330
4675+
# No check for non-string path_or-buf, and no test of open store
4676+
df = DataFrame(np.random.rand(4, 5),
4677+
index=list('abcd'),
4678+
columns=list('ABCDE'))
4679+
df.index.name = 'letters'
4680+
df = df.set_index(keys='E', append=True)
4681+
4682+
with ensure_clean_path(self.path) as path:
4683+
df.to_hdf(path, 'df', mode='w')
4684+
direct = read_hdf(path, 'df')
4685+
store = HDFStore(path, mode='r')
4686+
indirect = read_hdf(store, 'df')
4687+
tm.assert_frame_equal(direct, indirect)
4688+
self.assertTrue(store.is_open)
4689+
store.close()
4690+
4691+
def test_read_hdf_iterator(self):
4692+
df = DataFrame(np.random.rand(4, 5),
4693+
index=list('abcd'),
4694+
columns=list('ABCDE'))
4695+
df.index.name = 'letters'
4696+
df = df.set_index(keys='E', append=True)
4697+
4698+
with ensure_clean_path(self.path) as path:
4699+
df.to_hdf(path, 'df', mode='w', format='t')
4700+
direct = read_hdf(path, 'df')
4701+
iterator = read_hdf(path, 'df', iterator=True)
4702+
self.assertTrue(isinstance(iterator, TableIterator))
4703+
indirect = next(iterator.__iter__())
4704+
tm.assert_frame_equal(direct, indirect)
4705+
iterator.store.close()
4706+
4707+
def test_read_hdf_errors(self):
4708+
df = DataFrame(np.random.rand(4, 5),
4709+
index=list('abcd'),
4710+
columns=list('ABCDE'))
4711+
4712+
with ensure_clean_path(self.path) as path:
4713+
self.assertRaises(IOError, read_hdf, path, 'key')
4714+
df.to_hdf(path, 'df')
4715+
store = HDFStore(path, mode='r')
4716+
store.close()
4717+
self.assertRaises(IOError, read_hdf, store, 'df')
4718+
with open(path, mode='r') as store:
4719+
self.assertRaises(NotImplementedError, read_hdf, store, 'df')
4720+
46734721
def _test_sort(obj):
46744722
if isinstance(obj, DataFrame):
46754723
return obj.reindex(sorted(obj.index))

0 commit comments

Comments
 (0)