From 1babcd328708a5aa593da9791baca9c602cbe1ca Mon Sep 17 00:00:00 2001 From: Stefano Cianciulli Date: Sun, 15 Apr 2018 15:41:24 +0100 Subject: [PATCH 1/3] BUG: make HDFStore.select_column raise a KeyError when key is not a valid store --- pandas/io/pytables.py | 3 ++- pandas/tests/io/test_pytables.py | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index f9a496edb45a3..b303886f22175 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1094,7 +1094,8 @@ def get_storer(self, key): """ return the storer object for a key, raise if not in the file """ group = self.get_node(key) if group is None: - return None + raise KeyError('No object named {} in the file'.format(key)) + s = self._create_storer(group) s.infer_axes() return s diff --git a/pandas/tests/io/test_pytables.py b/pandas/tests/io/test_pytables.py index b34723d6cf72c..a6a38e005b9b6 100644 --- a/pandas/tests/io/test_pytables.py +++ b/pandas/tests/io/test_pytables.py @@ -3836,8 +3836,15 @@ def test_read_column(self): with ensure_clean_store(self.path) as store: _maybe_remove(store, 'df') - store.append('df', df) + # GH 17912 + # HDFStore.select_column should raise a KeyError + # exception if the key is not a valid store + with pytest.raises(KeyError, + message='No object named index in the file'): + store.select_column('df', 'index') + + store.append('df', df) # error pytest.raises(KeyError, store.select_column, 'df', 'foo') From 298a6fa4eeb7dcbaedd6da0531d32a0745871de8 Mon Sep 17 00:00:00 2001 From: Stefano Cianciulli Date: Sun, 15 Apr 2018 15:46:59 +0100 Subject: [PATCH 2/3] BUG: Make HDFStore.remove re-raise the KeyError coming from select_column when the key is not a valid store --- pandas/io/pytables.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index b303886f22175..4004a6ea8f6ff 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -887,7 +887,10 @@ def remove(self, key, where=None, start=None, stop=None): where = _ensure_term(where, scope_level=1) try: s = self.get_storer(key) - except: + except KeyError: + # the key is not a valid store, re-raising KeyError + raise + except Exception: if where is not None: raise ValueError( @@ -899,9 +902,6 @@ def remove(self, key, where=None, start=None, stop=None): s._f_remove(recursive=True) return None - if s is None: - raise KeyError('No object named %s in the file' % key) - # remove the node if com._all_none(where, start, stop): s.group._f_remove(recursive=True) From a9d4c6a3a9839815450292314a4a7d0a673c2d35 Mon Sep 17 00:00:00 2001 From: Stefano Cianciulli Date: Sun, 15 Apr 2018 15:59:55 +0100 Subject: [PATCH 3/3] DOC: Add entry in whatsnew for bug --- doc/source/whatsnew/v0.23.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index eee0f1997d081..8f7291bc1a28c 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -1111,6 +1111,7 @@ I/O - Bug in :meth:`pandas.io.json.json_normalize` where subrecords are not properly normalized if any subrecords values are NoneType (:issue:`20030`) - Bug in ``usecols`` parameter in :func:`pandas.io.read_csv` and :func:`pandas.io.read_table` where error is not raised correctly when passing a string. (:issue:`20529`) - Bug in :func:`HDFStore.keys` when reading a file with a softlink causes exception (:issue:`20523`) +- Bug in :func:`HDFStore.select_column` where a key which is not a valid store raised an ``AttributeError`` instead of a ``KeyError`` (:issue:`17912`) Plotting ^^^^^^^^