Skip to content

Commit 479e5a0

Browse files
item array method impl (#1015)
1 parent f3bf22e commit 479e5a0

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

dpnp/dpnp_array.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,40 @@ def flatten(self, order='C'):
465465

466466
# 'getfield',
467467
# 'imag',
468-
# 'item',
468+
469+
def item(self, id=None):
470+
"""
471+
Copy an element of an array to a standard Python scalar and return it.
472+
473+
For full documentation refer to :obj:`numpy.ndarray.item`.
474+
475+
Examples
476+
--------
477+
>>> np.random.seed(123)
478+
>>> x = np.random.randint(9, size=(3, 3))
479+
>>> x
480+
array([[2, 2, 6],
481+
[1, 3, 6],
482+
[1, 0, 1]])
483+
>>> x.item(3)
484+
1
485+
>>> x.item(7)
486+
0
487+
>>> x.item((0, 1))
488+
2
489+
>>> x.item((2, 2))
490+
1
491+
492+
"""
493+
494+
if id is None:
495+
if self.size != 1:
496+
raise ValueError("DPNP dparray::item(): can only convert an array of size 1 to a Python scalar")
497+
else:
498+
id = 0
499+
500+
return self.flat[id]
501+
469502
# 'itemset',
470503

471504
@property

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ tests/test_random.py::TestDistributionsMultinomial::test_seed
2222
tests/test_random.py::TestPermutationsTestShuffle::test_shuffle1[lambda x: dpnp.astype(dpnp.asarray(x), dpnp.int8)]
2323
tests/test_random.py::TestPermutationsTestShuffle::test_shuffle1[lambda x: dpnp.astype(dpnp.asarray(x), object)]
2424
tests/test_random.py::TestPermutationsTestShuffle::test_shuffle1[lambda x: dpnp.vstack([x, x]).T]
25-
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayItem_param_0_{shape=()}::test_item
26-
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayItem_param_1_{shape=(1,)}::test_item
27-
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayItem_param_2_{shape=(1, 1, 1)}::test_item
2825
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayItemRaise_param_0_{shape=(0,)}::test_item
2926
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayItemRaise_param_1_{shape=(2, 3)}::test_item
3027
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayItemRaise_param_2_{shape=(1, 0, 1)}::test_item

0 commit comments

Comments
 (0)