Skip to content

ENH: add basic DataFrame.from_arrow class method for importing through Arrow PyCapsule interface #59696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,42 @@ def closed(self) -> bool:
SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable])

SliceType = Optional[Hashable]


# Arrow PyCapsule Interface
# from https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html#protocol-typehints


class ArrowArrayExportable(Protocol):
"""
An object with an ``__arrow_c_array__`` method.

This method indicates the object is an Arrow-compatible object implementing
the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_ in
Python), enabling zero-copy Arrow data interchange across libraries.

.. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
.. _Arrow C Data Interface: https://arrow.apache.org/docs/format/CDataInterface.html

"""

def __arrow_c_array__(
self, requested_schema: object | None = None
) -> tuple[object, object]: ...


class ArrowStreamExportable(Protocol):
"""
An object with an ``__arrow_c_stream__`` method.

This method indicates the object is an Arrow-compatible object implementing
the `Arrow PyCapsule Protocol`_ (exposing the `Arrow C Data Interface`_
for streams in Python), enabling zero-copy Arrow data interchange across
libraries.

.. _Arrow PyCapsule Protocol: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html
.. _Arrow C Data Interface: https://arrow.apache.org/docs/format/CDataInterface.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe this should link to the stream interface page instead? https://arrow.apache.org/docs/format/CStreamInterface.html


"""

def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ...
6 changes: 5 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@
AnyAll,
AnyArrayLike,
ArrayLike,
ArrowArrayExportable,
ArrowStreamExportable,
Axes,
Axis,
AxisInt,
Expand Down Expand Up @@ -1747,7 +1749,9 @@ def __rmatmul__(self, other) -> DataFrame:
# IO methods (to / from other formats)

@classmethod
def from_arrow(cls, data) -> DataFrame:
def from_arrow(
cls, data: ArrowArrayExportable | ArrowStreamExportable
) -> DataFrame:
"""
Construct a DataFrame from a tabular Arrow object.

Expand Down
Loading