Skip to content

Commit

Permalink
fix: show informative error message if a non-existent column name is …
Browse files Browse the repository at this point in the history
…passed
  • Loading branch information
MarcoGorelli committed Aug 12, 2024
1 parent 0062e62 commit d80c24a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions altair/vegalite/v5/schema/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import TYPE_CHECKING, Any, Literal, Sequence, TypedDict, Union, overload
from typing_extensions import TypeAlias

from narwhals.dependencies import is_pandas_dataframe as _is_pandas_dataframe
import narwhals.stable.v1 as nw

from altair.utils import infer_encoding_types as _infer_encoding_types
from altair.utils import parse_shorthand
Expand Down Expand Up @@ -179,7 +179,7 @@ def to_dict(
# We still parse it out of the shorthand, but drop it here.
parsed.pop("type", None)
elif not (type_in_shorthand or type_defined_explicitly):
if _is_pandas_dataframe(context.get("data", None)):
if isinstance(context.get("data", None), nw.DataFrame):
msg = (
f'Unable to determine data type for the field "{shorthand}";'
" verify that the field name is not misspelled."
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/test_schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,17 @@ def test_multiple_field_strings_in_condition():
)


def test_non_existent_column_name():
df = pd.DataFrame({"a": [1, 2], "b": [4, 5]})
msg = (
'Unable to determine data type for the field "c"; verify that the field name '
"is not misspelled. If you are referencing a field from a transform, also "
"confirm that the data type is specified correctly."
)
with pytest.raises(ValueError, match=msg):
alt.Chart(df).mark_line().encode(x="a", y="c").to_json()


def test_serialize_numpy_types():
m = MySchema(
a={"date": np.datetime64("2019-01-01")},
Expand Down

0 comments on commit d80c24a

Please sign in to comment.