Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
76 changes: 76 additions & 0 deletions doc/reference/io.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
.. currentmodule:: dpnp

Input and output
================

.. hint:: `NumPy API Reference: Input and output <https://numpy.org/doc/stable/reference/routines.io.html>`_

.. 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
1 change: 1 addition & 0 deletions doc/reference/routines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ These functions cover a subset of
exceptions
fft
functional
io
indexing
linalg
logic
Expand Down
46 changes: 45 additions & 1 deletion dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 numpy as np
>>> a = np.array([1, 2])
>>> list(a)
[array(1), array(2)]
>>> a_tolist = 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):
"""
Expand Down
10 changes: 10 additions & 0 deletions dpnp/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions dpnp/tests/third_party/cupy/logic_tests/test_type_test.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()
)
Loading