|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import duckdb |
| 4 | +import polars as pl |
| 5 | +import pytest |
| 6 | + |
| 7 | +import narwhals.stable.v1 as nw |
| 8 | + |
| 9 | +data = {"a": [1, 2, 3], "b": [4.0, 5.0, 6.1], "z": ["x", "y", "z"]} |
| 10 | + |
| 11 | + |
| 12 | +def test_interchange() -> None: |
| 13 | + df_pl = pl.DataFrame(data) |
| 14 | + df = nw.from_native(df_pl.__dataframe__(), eager_or_interchange_only=True) |
| 15 | + with pytest.raises( |
| 16 | + NotImplementedError, |
| 17 | + match="Attribute select is not supported for metadata-only dataframes", |
| 18 | + ): |
| 19 | + df.select("a", "z") |
| 20 | + |
| 21 | + |
| 22 | +def test_interchange_ibis( |
| 23 | + tmpdir: pytest.TempdirFactory, |
| 24 | +) -> None: # pragma: no cover |
| 25 | + ibis = pytest.importorskip("ibis") |
| 26 | + df_pl = pl.DataFrame(data) |
| 27 | + |
| 28 | + filepath = str(tmpdir / "file.parquet") # type: ignore[operator] |
| 29 | + df_pl.write_parquet(filepath) |
| 30 | + |
| 31 | + tbl = ibis.read_parquet(filepath) |
| 32 | + df = nw.from_native(tbl, eager_or_interchange_only=True) |
| 33 | + |
| 34 | + out_cols = df.select("a", "z").schema.names() |
| 35 | + |
| 36 | + assert out_cols == ["a", "z"] |
| 37 | + |
| 38 | + |
| 39 | +def test_interchange_duckdb() -> None: |
| 40 | + df_pl = pl.DataFrame(data) # noqa: F841 |
| 41 | + rel = duckdb.sql("select * from df_pl") |
| 42 | + df = nw.from_native(rel, eager_or_interchange_only=True) |
| 43 | + |
| 44 | + out_cols = df.select("a", "z").schema.names() |
| 45 | + |
| 46 | + assert out_cols == ["a", "z"] |
0 commit comments