Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't materialise Ibis table to PyArrow if using vegafusion data transformer #3566

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 36 additions & 1 deletion 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 @@ -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"},
]