Skip to content

Commit 38e5ed8

Browse files
committed
Merge pull request #2443 from taldcroft/table-col-select
Improve error handling for bad column select
2 parents 69780d8 + 93194a3 commit 38e5ed8

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎astropy/table/table.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,12 @@ def __getitem__(self, item):
848848
return self.columns[item]
849849
elif isinstance(item, int):
850850
return self.Row(self, item)
851-
elif isinstance(item, (tuple, list)) and all(x in self.colnames
851+
elif isinstance(item, (tuple, list)) and all(isinstance(x, six.string_types)
852852
for x in item):
853+
bad_names = [x for x in item if x not in self.colnames]
854+
if bad_names:
855+
raise ValueError('Slice name(s) {0} not valid column name(s)'
856+
.format(', '.join(bad_names)))
853857
out = self.__class__([self[x] for x in item], meta=deepcopy(self.meta))
854858
out._groups = groups.TableGroups(out, indices=self.groups._indices,
855859
keys=self.groups._keys)

‎astropy/table/tests/test_item_access.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ def test_select_columns(self, table_data):
195195
assert t2.masked == self.t.masked
196196
assert t2._column_class == self.t._column_class
197197

198+
def test_select_columns_fail(self, table_data):
199+
"""Selecting a column that doesn't exist fails"""
200+
self.t = table_data.Table(table_data.COLS)
201+
202+
with pytest.raises(ValueError) as err:
203+
self.t[['xxxx']]
204+
assert 'Slice name(s) xxxx not valid column name(s)' in str(err)
205+
206+
with pytest.raises(ValueError) as err:
207+
self.t[['xxxx', 'yyyy']]
208+
assert 'Slice name(s) xxxx, yyyy not valid column name(s)' in str(err)
209+
198210
def test_np_where(self, table_data):
199211
"""Select rows using output of np.where"""
200212
t = table_data.Table(table_data.COLS)

0 commit comments

Comments
 (0)