Skip to content

Commit b527506

Browse files
krzysztof-kwittcpcloud
authored andcommitted
feat(ux): improve error message when column is not found
1 parent 41698ed commit b527506

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

ibis/backends/tests/test_generic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ def test_select_filter_mutate(backend, alltypes, df):
345345

346346

347347
def test_table_fillna_invalid(alltypes):
348-
with pytest.raises(com.IbisTypeError, match=r"'invalid_col' is not a field in.*"):
348+
with pytest.raises(
349+
com.IbisTypeError, match=r"Column 'invalid_col' is not found in table"
350+
):
349351
alltypes.fillna({'invalid_col': 0.0})
350352

351353
with pytest.raises(
@@ -412,7 +414,9 @@ def test_mutate_rename(alltypes):
412414

413415

414416
def test_dropna_invalid(alltypes):
415-
with pytest.raises(com.IbisTypeError, match=r"'invalid_col' is not a field in.*"):
417+
with pytest.raises(
418+
com.IbisTypeError, match=r"Column 'invalid_col' is not found in table"
419+
):
416420
alltypes.dropna(subset=['invalid_col'])
417421

418422
with pytest.raises(ValueError, match=r".*is not in.*"):

ibis/expr/operations/generic.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ def __init__(self, table, name):
3535
name = table.schema.name_at_position(name)
3636

3737
if name not in table.schema:
38-
raise com.IbisTypeError(f"value {name!r} is not a field in {table.schema}")
38+
columns_formatted = ', '.join(map(repr, table.schema.names))
39+
raise com.IbisTypeError(
40+
f"Column {name!r} is not found in table. "
41+
f"Existing columns: {columns_formatted}."
42+
)
3943

4044
super().__init__(table=table, name=name)
4145

ibis/expr/types/relations.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -956,12 +956,14 @@ def fillna(
956956

957957
if isinstance(replacements, collections.abc.Mapping):
958958
for col, val in replacements.items():
959-
try:
960-
col_type = schema[col]
961-
except KeyError:
959+
if col not in schema:
960+
columns_formatted = ', '.join(map(repr, schema.names))
962961
raise com.IbisTypeError(
963-
f'{col!r} is not a field in {schema}.'
962+
f"Column {col!r} is not found in table. "
963+
f"Existing columns: {columns_formatted}."
964964
) from None
965+
966+
col_type = schema[col]
965967
val_type = val.type() if isinstance(val, Expr) else dt.infer(val)
966968
if not dt.castable(val_type, col_type):
967969
raise com.IbisTypeError(

0 commit comments

Comments
 (0)