diff --git a/CHANGELOG.md b/CHANGELOG.md index dd0d169e1984..79feecded40c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum * Added implementation of `dpnp.scipy.special.erfcx` [#2596](https://github.com/IntelPython/dpnp/pull/2596) * Added implementation of `dpnp.scipy.special.erfinv` and `dpnp.scipy.special.erfcinv` [#2624](https://github.com/IntelPython/dpnp/pull/2624) * Enabled support of Python 3.14 [#2631](https://github.com/IntelPython/dpnp/pull/2631) +* Added implementation of `dpnp.ndarray.tolist` method [#2652](https://github.com/IntelPython/dpnp/pull/2652) ### Changed diff --git a/doc/reference/io.rst b/doc/reference/io.rst new file mode 100644 index 000000000000..b75159c92d32 --- /dev/null +++ b/doc/reference/io.rst @@ -0,0 +1,76 @@ +.. currentmodule:: dpnp + +Input and output +================ + +.. hint:: `NumPy API Reference: Input and output `_ + +.. NumPy binary files (npy, npz) +.. ----------------------------- +.. .. autosummary:: +.. :toctree: generated/ +.. :nosignatures: + +.. load +.. save +.. savez +.. savez_compressed +.. lib.npyio.NpzFile + +.. The format of these binary file types is documented in +.. :py:mod:`numpy.lib.format` + +Text files +---------- +.. autosummary:: + :toctree: generated/ + :nosignatures: + + loadtxt + savetxt + genfromtxt + fromregex + fromstring + ndarray.tofile + ndarray.tolist + +Raw binary files +---------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + + fromfile + ndarray.tofile + +.. String formatting +.. ----------------- +.. .. autosummary:: +.. :toctree: generated/ +.. :nosignatures: + +.. array2string +.. array_repr +.. array_str +.. format_float_positional +.. format_float_scientific + +.. Text formatting options +.. ----------------------- +.. .. autosummary:: +.. :toctree: generated/ +.. :nosignatures: + +.. set_printoptions +.. get_printoptions +.. printoptions + +Base-n representations +---------------------- +.. autosummary:: + :toctree: generated/ + :nosignatures: + + binary_repr + base_repr diff --git a/doc/reference/routines.rst b/doc/reference/routines.rst index e38eb159fd32..e8013e134273 100644 --- a/doc/reference/routines.rst +++ b/doc/reference/routines.rst @@ -18,6 +18,7 @@ These functions cover a subset of exceptions fft functional + io indexing linalg logic diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index beba058a998c..e8902a77eb13 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -2014,7 +2014,51 @@ def to_device(self, device, /, *, stream=None): # 'tobytes', # 'tofile', - # 'tolist', + + def tolist(self): + """ + Converts the array to a (possibly nested) Python list. + + For full documentation refer to :obj:`numpy.ndarray.tolist`. + + Returns + ------- + out : list + The possibly nested Python list of array elements. + + Examples + -------- + For a 1D array, ``a.tolist()`` is almost the same as ``list(a)``, + except that ``tolist`` changes 0D arrays to Python scalars: + + >>> import dpnp as np + >>> a = np.array([1, 2]) + >>> list(a) + [array(1), array(2)] + >>> a.tolist() + [1, 2] + + Additionally, for a 2D array, ``tolist`` applies recursively: + + >>> a = np.array([[1, 2], [3, 4]]) + >>> list(a) + [array([1, 2]), array([3, 4])] + >>> a.tolist() + [[1, 2], [3, 4]] + + The base case for this recursion is a 0D array: + + >>> a = np.array(1) + >>> list(a) + Traceback (most recent call last): + ... + TypeError: iteration over a 0-d array + >>> a.tolist() + 1 + + """ + + return self.asnumpy().tolist() def trace(self, offset=0, axis1=0, axis2=1, dtype=None, *, out=None): """ diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index ce857d73ea25..7aa7ac699e44 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -107,6 +107,16 @@ def test_strides(self): assert xp.full_like(a, fill_value=6) not in a +class TestToList: + @pytest.mark.parametrize( + "data", [[1, 2], [[1, 2], [3, 4]]], ids=["1d", "2d"] + ) + def test_basic(self, data): + a = numpy.array(data) + ia = dpnp.array(a) + assert_array_equal(ia.tolist(), a.tolist()) + + class TestView: def test_none_dtype(self): a = numpy.ones((1, 2, 4), dtype=numpy.int32) diff --git a/dpnp/tests/third_party/cupy/logic_tests/test_type_test.py b/dpnp/tests/third_party/cupy/logic_tests/test_type_test.py index ab1573a9b933..f4261a890f4c 100644 --- a/dpnp/tests/third_party/cupy/logic_tests/test_type_test.py +++ b/dpnp/tests/third_party/cupy/logic_tests/test_type_test.py @@ -1,9 +1,10 @@ +from __future__ import annotations + import unittest import numpy import pytest -import dpnp as cupy from dpnp.tests.third_party.cupy import testing @@ -122,7 +123,6 @@ def test_scalar(self, xp, dtype): @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_list(self, xp, dtype): - a = testing.shaped_arange((2, 3), xp, dtype) - if xp == cupy: - a = a.asnumpy() - return getattr(xp, self.func)(a.tolist()) + return getattr(xp, self.func)( + testing.shaped_arange((2, 3), xp, dtype).tolist() + )