Skip to content

Commit f835ff2

Browse files
MarcoGorelliammar-qazi
authored andcommitted
fix: use fastpath for PyCapsule export when starting from pyarrow-backed Series, respect requested_schema (pandas-dev#59683)
* fix: use fastpath for PyCapsule export when starting from pyarrow-backed Series, respect requested_schema * simplify * stringdtype test
1 parent 3dbaa4c commit f835ff2

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

pandas/core/series.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,15 @@ def __arrow_c_stream__(self, requested_schema=None):
580580
PyCapsule
581581
"""
582582
pa = import_optional_dependency("pyarrow", min_version="16.0.0")
583-
ca = pa.chunked_array([pa.Array.from_pandas(self, type=requested_schema)])
584-
return ca.__arrow_c_stream__(requested_schema)
583+
type = (
584+
pa.DataType._import_from_c_capsule(requested_schema)
585+
if requested_schema is not None
586+
else None
587+
)
588+
ca = pa.array(self, type=type)
589+
if not isinstance(ca, pa.ChunkedArray):
590+
ca = pa.chunked_array([ca])
591+
return ca.__arrow_c_stream__()
585592

586593
# ----------------------------------------------------------------------
587594

pandas/tests/series/test_arrow_interface.py

+38
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,41 @@ def test_series_arrow_interface():
2121
ca = pa.chunked_array(s)
2222
expected = pa.chunked_array([[1, 4, 2]])
2323
assert ca.equals(expected)
24+
ca = pa.chunked_array(s, type=pa.int32())
25+
expected = pa.chunked_array([[1, 4, 2]], type=pa.int32())
26+
assert ca.equals(expected)
27+
28+
29+
def test_series_arrow_interface_arrow_dtypes():
30+
s = pd.Series([1, 4, 2], dtype="Int64[pyarrow]")
31+
32+
capsule = s.__arrow_c_stream__()
33+
assert (
34+
ctypes.pythonapi.PyCapsule_IsValid(
35+
ctypes.py_object(capsule), b"arrow_array_stream"
36+
)
37+
== 1
38+
)
39+
40+
ca = pa.chunked_array(s)
41+
expected = pa.chunked_array([[1, 4, 2]])
42+
assert ca.equals(expected)
43+
ca = pa.chunked_array(s, type=pa.int32())
44+
expected = pa.chunked_array([[1, 4, 2]], type=pa.int32())
45+
assert ca.equals(expected)
46+
47+
48+
def test_series_arrow_interface_stringdtype():
49+
s = pd.Series(["foo", "bar"], dtype="string[pyarrow]")
50+
51+
capsule = s.__arrow_c_stream__()
52+
assert (
53+
ctypes.pythonapi.PyCapsule_IsValid(
54+
ctypes.py_object(capsule), b"arrow_array_stream"
55+
)
56+
== 1
57+
)
58+
59+
ca = pa.chunked_array(s)
60+
expected = pa.chunked_array([["foo", "bar"]], type=pa.large_string())
61+
assert ca.equals(expected)

0 commit comments

Comments
 (0)