Skip to content

Commit

Permalink
apacheGH-39217: [Python] RecordBatchReader.from_stream constructor fo…
Browse files Browse the repository at this point in the history
…r objects implementing the Arrow PyCapsule protocol
  • Loading branch information
jorisvandenbossche committed Dec 13, 2023
1 parent 4aa9f60 commit 38c5444
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions python/pyarrow/ipc.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,43 @@ cdef class RecordBatchReader(_Weakrefable):
self.reader = c_reader
return self

@staticmethod
def from_stream(data, schema=None):
"""
Create RecordBatchReader from a Arrow-compatible stream object.
This accepts objects implementing the Arrow PyCapsule Protocol for
streams, i.e. objects that have a ``__arrow_c_stream__`` method.
Parameters
----------
data : Arrow-compatible stream object
Any object that implements the Arrow PyCapsule Protocol for
streams.
schema : Schema, default None
The schema to which the stream should be casted, is supported
by the stream object.
Returns
-------
RecordBatchReader
"""

if not hasattr(data, "__arrow_c_stream__"):
raise TypeError(
"Expected an object implementing the Arrow PyCapsule Protocol for "
"streams (i.e. having a `__arrow_c_stream__` method), "
f"got {type(data)!r}."
)

if schema is not None:
requested = schema.__arrow_c_schema__()
else:
requested = None

capsule = data.__arrow_c_stream__(requested)
return RecordBatchReader._import_from_c_capsule(capsule)

@staticmethod
def from_batches(Schema schema not None, batches):
"""
Expand Down

0 comments on commit 38c5444

Please sign in to comment.