Skip to content

Commit 804101c

Browse files
CianciuStylesjreback
authored andcommitted
Fix issue 17912 (#20705)
HDFStore.select_column error reporting
1 parent d04b746 commit 804101c

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ I/O
11141114
- Bug in :meth:`pandas.io.json.json_normalize` where subrecords are not properly normalized if any subrecords values are NoneType (:issue:`20030`)
11151115
- Bug in ``usecols`` parameter in :func:`read_csv` where error is not raised correctly when passing a string. (:issue:`20529`)
11161116
- Bug in :func:`HDFStore.keys` when reading a file with a softlink causes exception (:issue:`20523`)
1117+
- Bug in :func:`HDFStore.select_column` where a key which is not a valid store raised an ``AttributeError`` instead of a ``KeyError`` (:issue:`17912`)
11171118

11181119
Plotting
11191120
^^^^^^^^

pandas/io/pytables.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,10 @@ def remove(self, key, where=None, start=None, stop=None):
887887
where = _ensure_term(where, scope_level=1)
888888
try:
889889
s = self.get_storer(key)
890-
except:
890+
except KeyError:
891+
# the key is not a valid store, re-raising KeyError
892+
raise
893+
except Exception:
891894

892895
if where is not None:
893896
raise ValueError(
@@ -899,9 +902,6 @@ def remove(self, key, where=None, start=None, stop=None):
899902
s._f_remove(recursive=True)
900903
return None
901904

902-
if s is None:
903-
raise KeyError('No object named %s in the file' % key)
904-
905905
# remove the node
906906
if com._all_none(where, start, stop):
907907
s.group._f_remove(recursive=True)
@@ -1094,7 +1094,8 @@ def get_storer(self, key):
10941094
""" return the storer object for a key, raise if not in the file """
10951095
group = self.get_node(key)
10961096
if group is None:
1097-
return None
1097+
raise KeyError('No object named {} in the file'.format(key))
1098+
10981099
s = self._create_storer(group)
10991100
s.infer_axes()
11001101
return s

pandas/tests/io/test_pytables.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3836,8 +3836,15 @@ def test_read_column(self):
38363836

38373837
with ensure_clean_store(self.path) as store:
38383838
_maybe_remove(store, 'df')
3839-
store.append('df', df)
38403839

3840+
# GH 17912
3841+
# HDFStore.select_column should raise a KeyError
3842+
# exception if the key is not a valid store
3843+
with pytest.raises(KeyError,
3844+
message='No object named index in the file'):
3845+
store.select_column('df', 'index')
3846+
3847+
store.append('df', df)
38413848
# error
38423849
pytest.raises(KeyError, store.select_column, 'df', 'foo')
38433850

0 commit comments

Comments
 (0)