Skip to content

Commit fad6bb1

Browse files
add type hints and protocol definitions
1 parent 6af237c commit fad6bb1

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

pandas/_typing.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,42 @@ def closed(self) -> bool:
528528
SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable])
529529

530530
SliceType = Optional[Hashable]
531+
532+
533+
# Arrow PyCapsule Interface
534+
# from https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html#protocol-typehints
535+
536+
537+
class ArrowArrayExportable(Protocol):
538+
"""
539+
An object with an ``__arrow_c_array__`` method.
540+
541+
This method indicates the object is an Arrow-compatible object implementing
542+
the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_ in
543+
Python), enabling zero-copy Arrow data interchange across libraries.
544+
545+
.. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
546+
.. _Arrow C Data Interface: https://arrow.apache.org/docs/format/CDataInterface.html
547+
548+
"""
549+
550+
def __arrow_c_array__(
551+
self, requested_schema: object | None = None
552+
) -> tuple[object, object]: ...
553+
554+
555+
class ArrowStreamExportable(Protocol):
556+
"""
557+
An object with an ``__arrow_c_stream__`` method.
558+
559+
This method indicates the object is an Arrow-compatible object implementing
560+
the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_
561+
for streams in Python), enabling zero-copy Arrow data interchange across
562+
libraries.
563+
564+
.. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
565+
.. _Arrow C Data Interface: https://arrow.apache.org/docs/format/CDataInterface.html
566+
567+
"""
568+
569+
def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ...

pandas/core/frame.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@
205205
AnyAll,
206206
AnyArrayLike,
207207
ArrayLike,
208+
ArrowArrayExportable,
209+
ArrowStreamExportable,
208210
Axes,
209211
Axis,
210212
AxisInt,
@@ -1747,7 +1749,9 @@ def __rmatmul__(self, other) -> DataFrame:
17471749
# IO methods (to / from other formats)
17481750

17491751
@classmethod
1750-
def from_arrow(cls, data) -> DataFrame:
1752+
def from_arrow(
1753+
cls, data: ArrowArrayExportable | ArrowStreamExportable
1754+
) -> DataFrame:
17511755
"""
17521756
Construct a DataFrame from a tabular Arrow object.
17531757

0 commit comments

Comments
 (0)