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

Function test_numpy_array_protocol

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

Source from the content-addressed store, hash-verified

3917
3918@pytest.mark.numpy
3919def test_numpy_array_protocol():
3920 # test the __array__ method on pyarrow.Array
3921 arr = pa.array([1, 2, 3])
3922 result = np.asarray(arr)
3923 expected = np.array([1, 2, 3], dtype="int64")
3924 np.testing.assert_array_equal(result, expected)
3925
3926 # this should not raise a deprecation warning with numpy 2.0+
3927 result = np.array(arr, copy=False)
3928 np.testing.assert_array_equal(result, expected)
3929
3930 result = np.array(arr, dtype="int64", copy=False)
3931 np.testing.assert_array_equal(result, expected)
3932
3933 # no zero-copy is possible
3934 arr = pa.array([1, 2, None])
3935 expected = np.array([1, 2, np.nan], dtype="float64")
3936 result = np.asarray(arr)
3937 np.testing.assert_array_equal(result, expected)
3938
3939 with pytest.raises(ValueError, match="Unable to avoid a copy"):
3940 np.array(arr, copy=False)
3941
3942 arr = pa.array([1, 2, 3])
3943 with pytest.raises(ValueError):
3944 np.array(arr, dtype="float64", copy=False)
3945
3946 # copy=True -> not yet passed by numpy, so we have to call this directly to test
3947 arr = pa.array([1, 2, 3])
3948 result = arr.__array__(copy=True)
3949 assert result.flags.writeable
3950
3951 arr = pa.array([1, 2, 3])
3952 result = arr.__array__(dtype=np.dtype("float64"), copy=True)
3953 assert result.dtype == "float64"
3954
3955
3956@pytest.mark.numpy

Callers

nothing calls this directly

Calls 2

dtypeMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected