MCPcopy Create free account
hub / github.com/apache/arrow / test_array_protocol

Function test_array_protocol

python/pyarrow/tests/test_array.py:3957–4021  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3955
3956@pytest.mark.numpy
3957def test_array_protocol():
3958
3959 class MyArray:
3960 def __init__(self, data):
3961 self.data = data
3962
3963 def __arrow_array__(self, type=None):
3964 return pa.array(self.data, type=type)
3965
3966 arr = MyArray(np.array([1, 2, 3], dtype='int64'))
3967 result = pa.array(arr)
3968 expected = pa.array([1, 2, 3], type=pa.int64())
3969 assert result.equals(expected)
3970 result = pa.array(arr, type=pa.int64())
3971 expected = pa.array([1, 2, 3], type=pa.int64())
3972 assert result.equals(expected)
3973 result = pa.array(arr, type=pa.float64())
3974 expected = pa.array([1, 2, 3], type=pa.float64())
3975 assert result.equals(expected)
3976
3977 # raise error when passing size or mask keywords
3978 with pytest.raises(ValueError):
3979 pa.array(arr, mask=np.array([True, False, True]))
3980 with pytest.raises(ValueError):
3981 pa.array(arr, size=3)
3982
3983 # ensure the return value is an Array
3984 class MyArrayInvalid:
3985 def __init__(self, data):
3986 self.data = data
3987
3988 def __arrow_array__(self, type=None):
3989 return np.array(self.data)
3990
3991 arr = MyArrayInvalid(np.array([1, 2, 3], dtype='int64'))
3992 with pytest.raises(TypeError):
3993 pa.array(arr)
3994
3995 # ARROW-7066 - allow ChunkedArray output
3996 # GH-33727 - if num_chunks=1 return Array
3997 class MyArray2:
3998 def __init__(self, data):
3999 self.data = data
4000
4001 def __arrow_array__(self, type=None):
4002 return pa.chunked_array([self.data], type=type)
4003
4004 arr = MyArray2(np.array([1, 2, 3], dtype='int64'))
4005 result = pa.array(arr)
4006 expected = pa.array([1, 2, 3], type=pa.int64())
4007 assert result.equals(expected)
4008
4009 class MyArray3:
4010 def __init__(self, data1, data2):
4011 self.data1 = data1
4012 self.data2 = data2
4013
4014 def __arrow_array__(self, type=None):

Callers

nothing calls this directly

Calls 7

MyArrayClass · 0.85
MyArrayInvalidClass · 0.85
MyArray2Class · 0.85
MyArray3Class · 0.85
equalsMethod · 0.80
chunked_arrayMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected