|
| 1 | +import duckdb |
| 2 | +import polars as pl |
| 3 | +import pytest |
| 4 | + |
| 5 | +import narwhals.stable.v1 as nw |
| 6 | + |
| 7 | +data = {"a": [1, 2, 3], "b": [4.5, 6.7, 8.9], "z": ["x", "y", "w"]} |
| 8 | + |
| 9 | + |
| 10 | +def test_interchange() -> None: |
| 11 | + df_pl = pl.DataFrame(data) |
| 12 | + df = nw.from_native(df_pl.__dataframe__(), eager_or_interchange_only=True) |
| 13 | + series = df["a"] |
| 14 | + |
| 15 | + with pytest.raises( |
| 16 | + NotImplementedError, |
| 17 | + match="Cannot access native namespace for metadata-only dataframes with unknown backend", |
| 18 | + ): |
| 19 | + df.__native_namespace__() |
| 20 | + |
| 21 | + with pytest.raises( |
| 22 | + NotImplementedError, |
| 23 | + match="Cannot access native namespace for metadata-only series with unknown backend", |
| 24 | + ): |
| 25 | + series.__native_namespace__() |
| 26 | + |
| 27 | + |
| 28 | +def test_ibis( |
| 29 | + tmpdir: pytest.TempdirFactory, |
| 30 | +) -> None: # pragma: no cover |
| 31 | + ibis = pytest.importorskip("ibis") |
| 32 | + df_pl = pl.DataFrame(data) |
| 33 | + |
| 34 | + filepath = str(tmpdir / "file.parquet") # type: ignore[operator] |
| 35 | + df_pl.write_parquet(filepath) |
| 36 | + tbl = ibis.read_parquet(filepath) |
| 37 | + df = nw.from_native(tbl, eager_or_interchange_only=True) |
| 38 | + series = df["a"] |
| 39 | + |
| 40 | + assert df.__native_namespace__() == ibis |
| 41 | + assert series.__native_namespace__() == ibis |
| 42 | + |
| 43 | + |
| 44 | +def test_duckdb() -> None: |
| 45 | + df_pl = pl.DataFrame(data) # noqa: F841 |
| 46 | + |
| 47 | + rel = duckdb.sql("select * from df_pl") |
| 48 | + df = nw.from_native(rel, eager_or_interchange_only=True) |
| 49 | + series = df["a"] |
| 50 | + |
| 51 | + assert df.__native_namespace__() == duckdb |
| 52 | + assert series.__native_namespace__() == duckdb |
0 commit comments