Skip to content
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

ARROW-3829: [Python] add __arrow_array__ protocol to support third-party array classes in conversion to Arrow #5106

Closed
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
8 changes: 6 additions & 2 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def array(object obj, type=None, mask=None, size=None, from_pandas=None,
else:
c_from_pandas = from_pandas

if _is_array_like(obj):
if hasattr(obj, '__arrow_array__'):
return obj.__arrow_array__(type=type)
Copy link

Choose a reason for hiding this comment

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

Would it make sense to check for a return value of NotImplemented and fall back to the default path?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so. Why would you define __arrow_array__ just to return NotImplemented?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think if you don't want to implement this, the user should simply not define __arrow_array__.

But we should probably add a check to ensure that was is returned from __arrow_array__ is actually a pyarrow Array (that is something that numpy also does).

Copy link
Member

Choose a reason for hiding this comment

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

Should we raise if non-default size and mask arguments are passed here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that sounds as a good idea.

elif _is_array_like(obj):
if mask is not None:
# out argument unused
mask = get_series_values(mask, &is_pandas_object)
Expand All @@ -178,7 +180,9 @@ def array(object obj, type=None, mask=None, size=None, from_pandas=None,
mask = values.mask
values = values.data

if pandas_api.is_categorical(values):
if hasattr(values, '__arrow_array__'):
return values.__arrow_array__(type=type)
Copy link
Member

Choose a reason for hiding this comment

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

Same here: raise if size or mask is given?

elif pandas_api.is_categorical(values):
return DictionaryArray.from_arrays(
values.codes, values.categories.values,
mask=mask, ordered=values.ordered,
Expand Down
6 changes: 4 additions & 2 deletions python/pyarrow/pandas-shim.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cdef class _PandasAPIShim(object):
object _loose_version, _version
object _pd, _types_api, _compat_module
object _data_frame, _index, _series, _categorical_type
object _datetimetz_type
object _datetimetz_type, _extension_array
object _array_like_types

def __init__(self):
Expand All @@ -54,9 +54,11 @@ cdef class _PandasAPIShim(object):
self._index = pd.Index
self._categorical_type = pd.Categorical
self._series = pd.Series
self._extension_array = pd.api.extensions.ExtensionArray

self._array_like_types = (self._series, self._index,
self._categorical_type)
self._categorical_type,
self._extension_array)

self._version = pd.__version__
from distutils.version import LooseVersion
Expand Down
21 changes: 21 additions & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,27 @@ def test_array_from_large_pyints():
pa.array([int(2 ** 63)])


def test_array_protocol():

class MyArray:
def __init__(self, data):
self.data = data

def __arrow_array__(self, type=None):
return pa.array(self.data, type=type)

arr = MyArray(np.array([1, 2, 3], dtype='int64'))
result = pa.array(arr)
expected = pa.array([1, 2, 3], type=pa.int64())
assert result.equals(expected)
result = pa.array(arr, type=pa.int64())
expected = pa.array([1, 2, 3], type=pa.int64())
assert result.equals(expected)
result = pa.array(arr, type=pa.float64())
expected = pa.array([1, 2, 3], type=pa.float64())
assert result.equals(expected)


def test_concat_array():
concatenated = pa.concat_arrays(
[pa.array([1, 2]), pa.array([3, 4])])
Expand Down
44 changes: 44 additions & 0 deletions python/pyarrow/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,50 @@ def test_variable_dictionary_with_pandas():
a.to_pandas()


# ----------------------------------------------------------------------
# Array protocol in pandas conversions tests


def test_array_protocol():

def __arrow_array__(self, type=None):
return pa.array(self._data, mask=self._mask, type=type)

df = pd.DataFrame({'a': pd.Series([1, 2, None], dtype='Int64')})
jorisvandenbossche marked this conversation as resolved.
Show resolved Hide resolved

# with latest pandas/arrow, trying to convert nullable integer errors
with pytest.raises(TypeError):
pa.table(df)

# patch IntegerArray with the protocol
pd.arrays.IntegerArray.__arrow_array__ = __arrow_array__

# default conversion
result = pa.table(df)
expected = pa.array([1, 2, None], pa.int64())
assert result[0].chunk(0).equals(expected)

# with specifying schema
schema = pa.schema([('a', pa.float64())])
result = pa.table(df, schema=schema)
expected2 = pa.array([1, 2, None], pa.float64())
assert result[0].chunk(0).equals(expected2)

# pass Series to pa.array
result = pa.array(df['a'])
assert result.equals(expected)
result = pa.array(df['a'], type=pa.float64())
assert result.equals(expected2)

# pass actual ExtensionArray to pa.array
result = pa.array(df['a'].values)
assert result.equals(expected)
result = pa.array(df['a'].values, type=pa.float64())
assert result.equals(expected2)

del pd.arrays.IntegerArray.__arrow_array__
Copy link

Choose a reason for hiding this comment

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

Maybe this could go in a try-finally to avoid a failure here possibly contaminating other tests.



# ----------------------------------------------------------------------
# Legacy metadata compatibility tests

Expand Down