Skip to content

Commit

Permalink
feat(ux): improve error message when column is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-kwitt authored and cpcloud committed Jan 22, 2023
1 parent 41698ed commit b527506
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
8 changes: 6 additions & 2 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ def test_select_filter_mutate(backend, alltypes, df):


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

with pytest.raises(
Expand Down Expand Up @@ -412,7 +414,9 @@ def test_mutate_rename(alltypes):


def test_dropna_invalid(alltypes):
with pytest.raises(com.IbisTypeError, match=r"'invalid_col' is not a field in.*"):
with pytest.raises(
com.IbisTypeError, match=r"Column 'invalid_col' is not found in table"
):
alltypes.dropna(subset=['invalid_col'])

with pytest.raises(ValueError, match=r".*is not in.*"):
Expand Down
6 changes: 5 additions & 1 deletion ibis/expr/operations/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def __init__(self, table, name):
name = table.schema.name_at_position(name)

if name not in table.schema:
raise com.IbisTypeError(f"value {name!r} is not a field in {table.schema}")
columns_formatted = ', '.join(map(repr, table.schema.names))
raise com.IbisTypeError(
f"Column {name!r} is not found in table. "
f"Existing columns: {columns_formatted}."
)

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

Expand Down
10 changes: 6 additions & 4 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,12 +956,14 @@ def fillna(

if isinstance(replacements, collections.abc.Mapping):
for col, val in replacements.items():
try:
col_type = schema[col]
except KeyError:
if col not in schema:
columns_formatted = ', '.join(map(repr, schema.names))
raise com.IbisTypeError(
f'{col!r} is not a field in {schema}.'
f"Column {col!r} is not found in table. "
f"Existing columns: {columns_formatted}."
) from None

col_type = schema[col]
val_type = val.type() if isinstance(val, Expr) else dt.infer(val)
if not dt.castable(val_type, col_type):
raise com.IbisTypeError(
Expand Down

0 comments on commit b527506

Please sign in to comment.