Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum
* Added implementation of `dpnp.ndarray.tolist` method [#2652](https://github.com/IntelPython/dpnp/pull/2652)
* Added implementation of `dpnp.frexp` [#2635](https://github.com/IntelPython/dpnp/pull/2635)
* Added implementation of `dpnp.ndarray.tofile` method [#2635](https://github.com/IntelPython/dpnp/pull/2635)
* Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658)
* Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658)
* Added implementation of `dpnp.ndarray.tobytes` method [#2656](https://github.com/IntelPython/dpnp/pull/2656)

### Changed

Expand Down
37 changes: 36 additions & 1 deletion dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,42 @@ def to_device(self, device, /, *, stream=None):
usm_res = self._array_obj.to_device(device, stream=stream)
return dpnp_array._create_from_usm_ndarray(usm_res)

# 'tobytes',
def tobytes(self, order="C"):
r"""
Constructs Python bytes containing the raw data bytes in the array.

For full documentation refer to :obj:`numpy.ndarray.tobytes`.

Parameters
----------
order : {None, "C", "F", "A", "K"}, optional
Controls the memory layout of the bytes object.

Default: ``"C"``.

Returns
-------
out : bytes
Python bytes exhibiting a copy of array's raw data.

See Also
--------
:obj:`dpnp.frombuffer` : Construct a 1D array from Python bytes.

Examples
--------
>>> import dpnp as np
>>> x = np.array([[0, 1], [2, 3]], dtype='i2')
>>> x.tobytes()
b'\x00\x00\x01\x00\x02\x00\x03\x00'
>>> x.tobytes("C") == x.tobytes()
True
>>> x.tobytes("F")
b'\x00\x00\x02\x00\x01\x00\x03\x00'

"""

return self.asnumpy().tobytes(order=order)

def tofile(self, fid, sep="", format=""):
"""
Expand Down
2 changes: 2 additions & 0 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,8 @@ def frombuffer(
:obj:`dpnp.fromfile` : Construct array from data in a text or binary file.
:obj:`dpnp.fromiter` : Construct array from an iterable object.
:obj:`dpnp.fromstring` : Construct array from the text data in a string.
:obj:`ndarray.tobytes` : Construct Python bytes from the raw data bytes
in the array.

Examples
--------
Expand Down
2 changes: 1 addition & 1 deletion dpnp/dpnp_iface_bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def binary_repr(num, width=None):

Examples
--------
>>> import numpy as np
>>> import dpnp as np
>>> np.binary_repr(3)
'11'
>>> np.binary_repr(-3)
Expand Down
2 changes: 1 addition & 1 deletion dpnp/dpnp_iface_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def compress(condition, a, axis=None, out=None):

Examples
--------
>>> import numpy as np
>>> import dpnp as np
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the same line in def poisson and def rayleigh as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was intended to keep that as it is now. The functions needs to be fully reworked and the whole docstrings have to be updated there, not only examples.

>>> a = np.array([[1, 2], [3, 4], [5, 6]])
>>> a
array([[1, 2],
Expand Down
12 changes: 12 additions & 0 deletions dpnp/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ def test_strides(self):
assert xp.full_like(a, fill_value=6) not in a


class TestToBytes:
@pytest.mark.parametrize("order", ["C", "F", "K", "A", None])
def test_roundtrip_binary_str(self, order):
x = generate_random_numpy_array((2, 4, 3), dtype=complex)
x[0, :, 1] = [numpy.nan, numpy.inf, -numpy.inf, numpy.nan]
a = dpnp.array(x)

s = a.tobytes(order=order)
b = dpnp.frombuffer(s, dtype=a.dtype)
assert_array_equal(b, a.asnumpy().flatten(order))


class TestToFile:
def _create_data(self):
x = generate_random_numpy_array((2, 4, 3), dtype=complex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import unittest

import numpy
Expand Down Expand Up @@ -42,7 +44,6 @@ def test_item(self):
{"shape": (2, 3), "order": "C"},
{"shape": (2, 3), "order": "F"},
)
@pytest.mark.skip("tobytes() method is not supported yet")
class TestNdarrayToBytes(unittest.TestCase):

@testing.for_all_dtypes()
Expand Down
Loading