|
34 | 34 | from pandas._libs.lib import is_range_indexer
|
35 | 35 | from pandas.compat import PYPY
|
36 | 36 | from pandas.compat._constants import REF_COUNT
|
| 37 | +from pandas.compat._optional import import_optional_dependency |
37 | 38 | from pandas.compat.numpy import function as nv
|
38 | 39 | from pandas.errors import (
|
39 | 40 | ChainedAssignmentError,
|
@@ -558,6 +559,39 @@ def _init_dict(
|
558 | 559 |
|
559 | 560 | # ----------------------------------------------------------------------
|
560 | 561 |
|
| 562 | + def __arrow_c_stream__(self, requested_schema=None): |
| 563 | + """ |
| 564 | + Export the pandas Series as an Arrow C stream PyCapsule. |
| 565 | +
|
| 566 | + This relies on pyarrow to convert the pandas Series to the Arrow |
| 567 | + format (and follows the default behaviour of ``pyarrow.Array.from_pandas`` |
| 568 | + in its handling of the index, i.e. to ignore it). |
| 569 | + This conversion is not necessarily zero-copy. |
| 570 | +
|
| 571 | + Parameters |
| 572 | + ---------- |
| 573 | + requested_schema : PyCapsule, default None |
| 574 | + The schema to which the dataframe should be casted, passed as a |
| 575 | + PyCapsule containing a C ArrowSchema representation of the |
| 576 | + requested schema. |
| 577 | +
|
| 578 | + Returns |
| 579 | + ------- |
| 580 | + PyCapsule |
| 581 | + """ |
| 582 | + pa = import_optional_dependency("pyarrow", min_version="16.0.0") |
| 583 | + if requested_schema is not None: |
| 584 | + # todo: how should this be supported? |
| 585 | + msg = ( |
| 586 | + "Passing `requested_schema` to `Series.__arrow_c_stream__` is not yet " |
| 587 | + "supported" |
| 588 | + ) |
| 589 | + raise NotImplementedError(msg) |
| 590 | + ca = pa.chunked_array([pa.Array.from_pandas(self, type=requested_schema)]) |
| 591 | + return ca.__arrow_c_stream__() |
| 592 | + |
| 593 | + # ---------------------------------------------------------------------- |
| 594 | + |
561 | 595 | @property
|
562 | 596 | def _constructor(self) -> type[Series]:
|
563 | 597 | return Series
|
|
0 commit comments