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

Function test_numpy_array_protocol

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

Source from the content-addressed store, hash-verified

3725
3726@pytest.mark.numpy
3727def test_numpy_array_protocol():
3728 # test the __array__ method on pyarrow.Array
3729 arr = pa.array([1, 2, 3])
3730 result = np.asarray(arr)
3731 expected = np.array([1, 2, 3], dtype="int64")
3732 np.testing.assert_array_equal(result, expected)
3733
3734 # this should not raise a deprecation warning with numpy 2.0+
3735 result = np.array(arr, copy=False)
3736 np.testing.assert_array_equal(result, expected)
3737
3738 result = np.array(arr, dtype="int64", copy=False)
3739 np.testing.assert_array_equal(result, expected)
3740
3741 # no zero-copy is possible
3742 arr = pa.array([1, 2, None])
3743 expected = np.array([1, 2, np.nan], dtype="float64")
3744 result = np.asarray(arr)
3745 np.testing.assert_array_equal(result, expected)
3746
3747 if Version(np.__version__) < Version("2.0.0.dev0"):
3748 # copy keyword is not strict and not passed down to __array__
3749 result = np.array(arr, copy=False)
3750 np.testing.assert_array_equal(result, expected)
3751
3752 result = np.array(arr, dtype="float64", copy=False)
3753 np.testing.assert_array_equal(result, expected)
3754 else:
3755 # starting with numpy 2.0, the copy=False keyword is assumed to be strict
3756 with pytest.raises(ValueError, match="Unable to avoid a copy"):
3757 np.array(arr, copy=False)
3758
3759 arr = pa.array([1, 2, 3])
3760 with pytest.raises(ValueError):
3761 np.array(arr, dtype="float64", copy=False)
3762
3763 # copy=True -> not yet passed by numpy, so we have to call this directly to test
3764 arr = pa.array([1, 2, 3])
3765 result = arr.__array__(copy=True)
3766 assert result.flags.writeable
3767
3768 arr = pa.array([1, 2, 3])
3769 result = arr.__array__(dtype=np.dtype("float64"), copy=True)
3770 assert result.dtype == "float64"
3771
3772
3773@pytest.mark.numpy

Callers

nothing calls this directly

Calls 3

VersionClass · 0.90
dtypeMethod · 0.80
arrayMethod · 0.45

Tested by

no test coverage detected