From d80c24ab6f65d0ca60b789d10d40cb6fc6739f84 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 12 Aug 2024 10:16:36 +0100 Subject: [PATCH] fix: show informative error message if a non-existent column name is passed --- altair/vegalite/v5/schema/channels.py | 4 ++-- tests/utils/test_schemapi.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/altair/vegalite/v5/schema/channels.py b/altair/vegalite/v5/schema/channels.py index 34daf00ab..bf0008e9b 100644 --- a/altair/vegalite/v5/schema/channels.py +++ b/altair/vegalite/v5/schema/channels.py @@ -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 @@ -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." diff --git a/tests/utils/test_schemapi.py b/tests/utils/test_schemapi.py index acea9dcb6..4d741ca60 100644 --- a/tests/utils/test_schemapi.py +++ b/tests/utils/test_schemapi.py @@ -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")},