Skip to content

Commit

Permalink
fix: dont materialise Ibis table to PyArrow if using vegafusion data
Browse files Browse the repository at this point in the history
transformer
  • Loading branch information
MarcoGorelli committed Sep 3, 2024
1 parent 5b58779 commit b732650
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
5 changes: 1 addition & 4 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
39 changes: 37 additions & 2 deletions tests/vegalite/v5/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]}
)
Expand All @@ -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"},
]

0 comments on commit b732650

Please sign in to comment.