Skip to content

Commit 1af66e5

Browse files
committed
Implement tobytes() method in dpnp.ndarray
1 parent 03e0975 commit 1af66e5

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

dpnp/dpnp_array.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,42 @@ def to_device(self, device, /, *, stream=None):
20122012
usm_res = self._array_obj.to_device(device, stream=stream)
20132013
return dpnp_array._create_from_usm_ndarray(usm_res)
20142014

2015-
# 'tobytes',
2015+
def tobytes(self, order="C"):
2016+
r"""
2017+
Constructs Python bytes containing the raw data bytes in the array.
2018+
2019+
For full documentation refer to :obj:`numpy.ndarray.tobytes`.
2020+
2021+
Parameters
2022+
----------
2023+
order : {None, "C", "F", "A", "K"}, optional
2024+
Controls the memory layout of the bytes object.
2025+
2026+
Default: ``"C"``.
2027+
2028+
Returns
2029+
-------
2030+
out : bytes
2031+
Python bytes exhibiting a copy of array's raw data.
2032+
2033+
See Also
2034+
--------
2035+
:obj:`dpnp.frombuffer` : Construct a 1D array from Python bytes.
2036+
2037+
Examples
2038+
--------
2039+
>>> import dpnp as np
2040+
>>> x = np.array([[0, 1], [2, 3]], dtype='i2')
2041+
>>> x.tobytes()
2042+
b'\x00\x00\x01\x00\x02\x00\x03\x00'
2043+
>>> x.tobytes("C") == x.tobytes()
2044+
True
2045+
>>> x.tobytes("F")
2046+
b'\x00\x00\x02\x00\x01\x00\x03\x00'
2047+
2048+
"""
2049+
2050+
return self.asnumpy().tobytes(order=order)
20162051

20172052
def tofile(self, fid, sep="", format=""):
20182053
"""

dpnp/dpnp_iface_arraycreation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,8 @@ def frombuffer(
17051705
:obj:`dpnp.fromfile` : Construct array from data in a text or binary file.
17061706
:obj:`dpnp.fromiter` : Construct array from an iterable object.
17071707
:obj:`dpnp.fromstring` : Construct array from the text data in a string.
1708+
:obj:`ndarray.tobytes` : Construct Python bytes from the raw data bytes
1709+
in the array.
17081710
17091711
Examples
17101712
--------

dpnp/dpnp_iface_bitwise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def binary_repr(num, width=None):
111111
112112
Examples
113113
--------
114-
>>> import numpy as np
114+
>>> import dpnp as np
115115
>>> np.binary_repr(3)
116116
'11'
117117
>>> np.binary_repr(-3)

dpnp/dpnp_iface_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def compress(condition, a, axis=None, out=None):
396396
397397
Examples
398398
--------
399-
>>> import numpy as np
399+
>>> import dpnp as np
400400
>>> a = np.array([[1, 2], [3, 4], [5, 6]])
401401
>>> a
402402
array([[1, 2],

0 commit comments

Comments
 (0)