-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Support arrow/parquet roundtrip for nullable integer / string extension dtypes #29483
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
Changes from all commits
4e97e05
27dcbfb
72f4142
62ca041
38218ad
0521875
e593f9e
c317aab
b973eb3
5290afc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,6 +251,48 @@ To use a test, subclass it: | |
See https://github.com/pandas-dev/pandas/blob/master/pandas/tests/extension/base/__init__.py | ||
for a list of all the tests available. | ||
|
||
.. _extending.extension.arrow: | ||
|
||
Compatibility with Apache Arrow | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
An ``ExtensionArray`` can support conversion to / from ``pyarrow`` arrays | ||
(and thus support for example serialization to the Parquet file format) | ||
by implementing two methods: ``ExtensionArray.__arrow_array__`` and | ||
``ExtensionDtype.__from_arrow__``. | ||
|
||
The ``ExtensionArray.__arrow_array__`` ensures that ``pyarrow`` knowns how | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: knowns -> knows |
||
to convert the specific extension array into a ``pyarrow.Array`` (also when | ||
included as a column in a pandas DataFrame): | ||
|
||
.. code-block:: python | ||
|
||
class MyExtensionArray(ExtensionArray): | ||
... | ||
|
||
def __arrow_array__(self, type=None): | ||
# convert the underlying array values to a pyarrow Array | ||
import pyarrow | ||
return pyarrow.array(..., type=type) | ||
|
||
The ``ExtensionDtype.__from_arrow__`` method then controls the conversion | ||
back from pyarrow to a pandas ExtensionArray. This method receives a pyarrow | ||
``Array`` or ``ChunkedArray`` as only argument and is expected to return the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "as only" -> "as its only" |
||
appropriate pandas ``ExtensionArray`` for this dtype and the passed values: | ||
|
||
.. code-block:: none | ||
|
||
class ExtensionDtype: | ||
... | ||
|
||
def __from_arrow__(self, array: pyarrow.Array/ChunkedArray) -> ExtensionArray: | ||
... | ||
|
||
See more in the `Arrow documentation <https://arrow.apache.org/docs/python/extending_types.html>`__. | ||
|
||
Those methods have been implemented for the nullable integer and string extension | ||
dtypes included in pandas, and ensure roundtrip to pyarrow and the Parquet file format. | ||
|
||
.. _extension dtype dtypes: https://github.com/pandas-dev/pandas/blob/master/pandas/core/dtypes/dtypes.py | ||
.. _extension dtype source: https://github.com/pandas-dev/pandas/blob/master/pandas/core/dtypes/base.py | ||
.. _extension array source: https://github.com/pandas-dev/pandas/blob/master/pandas/core/arrays/base.py | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,35 @@ def construct_array_type(cls): | |
""" | ||
return IntegerArray | ||
|
||
def __from_arrow__(self, array): | ||
"""Construct IntegerArray from passed pyarrow Array/ChunkedArray""" | ||
import pyarrow | ||
|
||
if isinstance(array, pyarrow.Array): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When is this False? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The passed pyarrow values can be either a pyarrow.Array or pyarrow.ChunkedArray. Added a comment for this |
||
chunks = [array] | ||
else: | ||
# pyarrow.ChunkedArray | ||
chunks = array.chunks | ||
|
||
results = [] | ||
for arr in chunks: | ||
buflist = arr.buffers() | ||
data = np.frombuffer(buflist[1], dtype=self.type)[ | ||
arr.offset : arr.offset + len(arr) | ||
] | ||
bitmask = buflist[0] | ||
if bitmask is not None: | ||
mask = pyarrow.BooleanArray.from_buffers( | ||
pyarrow.bool_(), len(arr), [None, bitmask] | ||
) | ||
mask = np.asarray(mask) | ||
else: | ||
mask = np.ones(len(arr), dtype=bool) | ||
int_arr = IntegerArray(data.copy(), ~mask, copy=False) | ||
results.append(int_arr) | ||
|
||
return IntegerArray._concat_same_type(results) | ||
|
||
|
||
def integer_array(values, dtype=None, copy=False): | ||
""" | ||
|
Uh oh!
There was an error while loading. Please reload this page.