-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add from_arrow (which uses the PyCapsule Interface) (#1181)
- Loading branch information
1 parent
9b628ee
commit 879d3cf
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import sys | ||
|
||
import pandas as pd | ||
import polars as pl | ||
import pyarrow as pa | ||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
from narwhals.utils import parse_version | ||
from tests.utils import compare_dicts | ||
|
||
|
||
@pytest.mark.xfail(parse_version(pa.__version__) < (14,), reason="too old") | ||
def test_from_arrow_to_arrow() -> None: | ||
df = nw.from_native(pl.DataFrame({"ab": [1, 2, 3], "ba": [4, 5, 6]}), eager_only=True) | ||
result = nw.from_arrow(df, native_namespace=pa) | ||
assert isinstance(result.to_native(), pa.Table) | ||
expected = {"ab": [1, 2, 3], "ba": [4, 5, 6]} | ||
compare_dicts(result, expected) | ||
|
||
|
||
@pytest.mark.xfail(parse_version(pa.__version__) < (14,), reason="too old") | ||
def test_from_arrow_to_polars(monkeypatch: pytest.MonkeyPatch) -> None: | ||
tbl = pa.table({"ab": [1, 2, 3], "ba": [4, 5, 6]}) | ||
monkeypatch.delitem(sys.modules, "pandas") | ||
df = nw.from_native(tbl, eager_only=True) | ||
result = nw.from_arrow(df, native_namespace=pl) | ||
assert isinstance(result.to_native(), pl.DataFrame) | ||
expected = {"ab": [1, 2, 3], "ba": [4, 5, 6]} | ||
compare_dicts(result, expected) | ||
assert "pandas" not in sys.modules | ||
|
||
|
||
@pytest.mark.xfail(parse_version(pa.__version__) < (14,), reason="too old") | ||
def test_from_arrow_to_pandas() -> None: | ||
df = nw.from_native(pa.table({"ab": [1, 2, 3], "ba": [4, 5, 6]}), eager_only=True) | ||
result = nw.from_arrow(df, native_namespace=pd) | ||
assert isinstance(result.to_native(), pd.DataFrame) | ||
expected = {"ab": [1, 2, 3], "ba": [4, 5, 6]} | ||
compare_dicts(result, expected) | ||
|
||
|
||
def test_from_arrow_invalid() -> None: | ||
with pytest.raises(TypeError, match="PyCapsule"): | ||
nw.from_arrow({"a": [1]}, native_namespace=pa) # type: ignore[arg-type] |