diff --git a/altair/vegalite/v5/api.py b/altair/vegalite/v5/api.py index 9ef5659ba..95b670d2f 100644 --- a/altair/vegalite/v5/api.py +++ b/altair/vegalite/v5/api.py @@ -32,9 +32,6 @@ compile_with_vegafusion as _compile_with_vegafusion, ) from altair.utils._vegafusion_data import using_vegafusion as _using_vegafusion -from altair.utils.core import ( - to_eager_narwhals_dataframe as _to_eager_narwhals_dataframe, -) from altair.utils.data import DataType from altair.utils.data import is_data_type as _is_data_type @@ -1807,7 +1804,7 @@ def to_dict( # noqa: C901 original_data = getattr(copy, "data", Undefined) if not utils.is_undefined(original_data): try: - data = _to_eager_narwhals_dataframe(original_data) + data = nw.from_native(original_data, eager_or_interchange_only=True) except TypeError: # Non-narwhalifiable type supported by Altair, such as dict data = original_data diff --git a/tests/vegalite/v5/test_api.py b/tests/vegalite/v5/test_api.py index 4f54034d6..821abbf63 100644 --- a/tests/vegalite/v5/test_api.py +++ b/tests/vegalite/v5/test_api.py @@ -10,7 +10,7 @@ import re import sys import tempfile -from datetime import date +from datetime import date, datetime from importlib.metadata import version as importlib_version import ibis @@ -1537,7 +1537,7 @@ def test_polars_with_pandas_nor_pyarrow(monkeypatch: pytest.MonkeyPatch): @pytest.mark.xfail( sys.platform == "win32", reason="Timezone database is not installed on Windows" ) -def test_ibis_with_date_32(): +def test_ibis_with_date_32(monkeypatch: pytest.MonkeyPatch): df = pl.DataFrame( {"a": [1, 2, 3], "b": [date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3)]} ) @@ -1548,3 +1548,38 @@ def test_ibis_with_date_32(): {"a": 2, "b": "2020-01-02T00:00:00"}, {"a": 3, "b": "2020-01-03T00:00:00"}, ] + + +@pytest.mark.skipif( + sys.version_info < (3, 9), + reason="The maximum `ibis` version installable on Python 3.8 is `ibis==5.1.0`," + " which doesn't support the dataframe interchange protocol.", +) +@pytest.mark.skipif( + Version("1.5") > PANDAS_VERSION, + reason="A warning is thrown on old pandas versions", +) +@pytest.mark.xfail( + sys.platform == "win32", reason="Timezone database is not installed on Windows" +) +def test_ibis_with_vegafusion(monkeypatch: pytest.MonkeyPatch): + df = pl.DataFrame( + { + "a": [1, 2, 3], + "b": [datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3)], + } + ) + tbl = ibis.memtable(df) + # "poison" `arrow_table_from_dfi_dataframe` to check that it does not get called + # if we use the vegafusion transformer + monkeypatch.setattr( + "altair.utils.data.arrow_table_from_dfi_dataframe", lambda x: 1 / 0 + ) + tbl = ibis.memtable(df) + with alt.data_transformers.enable("vegafusion"): + result = alt.Chart(tbl).mark_line().encode(x="a", y="b").to_dict(format="vega") + assert next(iter(result["data"]))["values"] == [ + {"a": 1, "b": "2020-01-01T00:00:00.000"}, + {"a": 2, "b": "2020-01-02T00:00:00.000"}, + {"a": 3, "b": "2020-01-03T00:00:00.000"}, + ]