-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: support the Arrow PyCapsule Interface on pandas.DataFrame (expor…
…t) (#56587) * ENH: support the Arrow PyCapsule Interface on pandas.DataFrame * expand documentation on how index is handled
- Loading branch information
1 parent
e544b9f
commit 7212ecd
Showing
4 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import ctypes | ||
|
||
import pytest | ||
|
||
import pandas.util._test_decorators as td | ||
|
||
import pandas as pd | ||
|
||
pa = pytest.importorskip("pyarrow") | ||
|
||
|
||
@td.skip_if_no("pyarrow", min_version="14.0") | ||
def test_dataframe_arrow_interface(): | ||
df = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}) | ||
|
||
capsule = df.__arrow_c_stream__() | ||
assert ( | ||
ctypes.pythonapi.PyCapsule_IsValid( | ||
ctypes.py_object(capsule), b"arrow_array_stream" | ||
) | ||
== 1 | ||
) | ||
|
||
table = pa.table(df) | ||
expected = pa.table({"a": [1, 2, 3], "b": ["a", "b", "c"]}) | ||
assert table.equals(expected) | ||
|
||
schema = pa.schema([("a", pa.int8()), ("b", pa.string())]) | ||
table = pa.table(df, schema=schema) | ||
expected = expected.cast(schema) | ||
assert table.equals(expected) | ||
|
||
|
||
@td.skip_if_no("pyarrow", min_version="15.0") | ||
def test_dataframe_to_arrow(): | ||
df = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}) | ||
|
||
table = pa.RecordBatchReader.from_stream(df) | ||
expected = pa.table({"a": [1, 2, 3], "b": ["a", "b", "c"]}) | ||
assert table.equals(expected) | ||
|
||
schema = pa.schema([("a", pa.int8()), ("b", pa.string())]) | ||
table = pa.RecordBatchReader.from_stream(df, schema=schema) | ||
expected = expected.cast(schema) | ||
assert table.equals(expected) |
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