test_indexing.py 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. import sys
  2. import warnings
  3. import functools
  4. import operator
  5. import pytest
  6. import numpy as np
  7. from numpy.core._multiarray_tests import array_indexing
  8. from itertools import product
  9. from numpy.testing import (
  10. assert_, assert_equal, assert_raises, assert_raises_regex,
  11. assert_array_equal, assert_warns, HAS_REFCOUNT, IS_WASM
  12. )
  13. class TestIndexing:
  14. def test_index_no_floats(self):
  15. a = np.array([[[5]]])
  16. assert_raises(IndexError, lambda: a[0.0])
  17. assert_raises(IndexError, lambda: a[0, 0.0])
  18. assert_raises(IndexError, lambda: a[0.0, 0])
  19. assert_raises(IndexError, lambda: a[0.0,:])
  20. assert_raises(IndexError, lambda: a[:, 0.0])
  21. assert_raises(IndexError, lambda: a[:, 0.0,:])
  22. assert_raises(IndexError, lambda: a[0.0,:,:])
  23. assert_raises(IndexError, lambda: a[0, 0, 0.0])
  24. assert_raises(IndexError, lambda: a[0.0, 0, 0])
  25. assert_raises(IndexError, lambda: a[0, 0.0, 0])
  26. assert_raises(IndexError, lambda: a[-1.4])
  27. assert_raises(IndexError, lambda: a[0, -1.4])
  28. assert_raises(IndexError, lambda: a[-1.4, 0])
  29. assert_raises(IndexError, lambda: a[-1.4,:])
  30. assert_raises(IndexError, lambda: a[:, -1.4])
  31. assert_raises(IndexError, lambda: a[:, -1.4,:])
  32. assert_raises(IndexError, lambda: a[-1.4,:,:])
  33. assert_raises(IndexError, lambda: a[0, 0, -1.4])
  34. assert_raises(IndexError, lambda: a[-1.4, 0, 0])
  35. assert_raises(IndexError, lambda: a[0, -1.4, 0])
  36. assert_raises(IndexError, lambda: a[0.0:, 0.0])
  37. assert_raises(IndexError, lambda: a[0.0:, 0.0,:])
  38. def test_slicing_no_floats(self):
  39. a = np.array([[5]])
  40. # start as float.
  41. assert_raises(TypeError, lambda: a[0.0:])
  42. assert_raises(TypeError, lambda: a[0:, 0.0:2])
  43. assert_raises(TypeError, lambda: a[0.0::2, :0])
  44. assert_raises(TypeError, lambda: a[0.0:1:2,:])
  45. assert_raises(TypeError, lambda: a[:, 0.0:])
  46. # stop as float.
  47. assert_raises(TypeError, lambda: a[:0.0])
  48. assert_raises(TypeError, lambda: a[:0, 1:2.0])
  49. assert_raises(TypeError, lambda: a[:0.0:2, :0])
  50. assert_raises(TypeError, lambda: a[:0.0,:])
  51. assert_raises(TypeError, lambda: a[:, 0:4.0:2])
  52. # step as float.
  53. assert_raises(TypeError, lambda: a[::1.0])
  54. assert_raises(TypeError, lambda: a[0:, :2:2.0])
  55. assert_raises(TypeError, lambda: a[1::4.0, :0])
  56. assert_raises(TypeError, lambda: a[::5.0,:])
  57. assert_raises(TypeError, lambda: a[:, 0:4:2.0])
  58. # mixed.
  59. assert_raises(TypeError, lambda: a[1.0:2:2.0])
  60. assert_raises(TypeError, lambda: a[1.0::2.0])
  61. assert_raises(TypeError, lambda: a[0:, :2.0:2.0])
  62. assert_raises(TypeError, lambda: a[1.0:1:4.0, :0])
  63. assert_raises(TypeError, lambda: a[1.0:5.0:5.0,:])
  64. assert_raises(TypeError, lambda: a[:, 0.4:4.0:2.0])
  65. # should still get the DeprecationWarning if step = 0.
  66. assert_raises(TypeError, lambda: a[::0.0])
  67. def test_index_no_array_to_index(self):
  68. # No non-scalar arrays.
  69. a = np.array([[[1]]])
  70. assert_raises(TypeError, lambda: a[a:a:a])
  71. def test_none_index(self):
  72. # `None` index adds newaxis
  73. a = np.array([1, 2, 3])
  74. assert_equal(a[None], a[np.newaxis])
  75. assert_equal(a[None].ndim, a.ndim + 1)
  76. def test_empty_tuple_index(self):
  77. # Empty tuple index creates a view
  78. a = np.array([1, 2, 3])
  79. assert_equal(a[()], a)
  80. assert_(a[()].base is a)
  81. a = np.array(0)
  82. assert_(isinstance(a[()], np.int_))
  83. def test_void_scalar_empty_tuple(self):
  84. s = np.zeros((), dtype='V4')
  85. assert_equal(s[()].dtype, s.dtype)
  86. assert_equal(s[()], s)
  87. assert_equal(type(s[...]), np.ndarray)
  88. def test_same_kind_index_casting(self):
  89. # Indexes should be cast with same-kind and not safe, even if that
  90. # is somewhat unsafe. So test various different code paths.
  91. index = np.arange(5)
  92. u_index = index.astype(np.uintp)
  93. arr = np.arange(10)
  94. assert_array_equal(arr[index], arr[u_index])
  95. arr[u_index] = np.arange(5)
  96. assert_array_equal(arr, np.arange(10))
  97. arr = np.arange(10).reshape(5, 2)
  98. assert_array_equal(arr[index], arr[u_index])
  99. arr[u_index] = np.arange(5)[:,None]
  100. assert_array_equal(arr, np.arange(5)[:,None].repeat(2, axis=1))
  101. arr = np.arange(25).reshape(5, 5)
  102. assert_array_equal(arr[u_index, u_index], arr[index, index])
  103. def test_empty_fancy_index(self):
  104. # Empty list index creates an empty array
  105. # with the same dtype (but with weird shape)
  106. a = np.array([1, 2, 3])
  107. assert_equal(a[[]], [])
  108. assert_equal(a[[]].dtype, a.dtype)
  109. b = np.array([], dtype=np.intp)
  110. assert_equal(a[[]], [])
  111. assert_equal(a[[]].dtype, a.dtype)
  112. b = np.array([])
  113. assert_raises(IndexError, a.__getitem__, b)
  114. def test_ellipsis_index(self):
  115. a = np.array([[1, 2, 3],
  116. [4, 5, 6],
  117. [7, 8, 9]])
  118. assert_(a[...] is not a)
  119. assert_equal(a[...], a)
  120. # `a[...]` was `a` in numpy <1.9.
  121. assert_(a[...].base is a)
  122. # Slicing with ellipsis can skip an
  123. # arbitrary number of dimensions
  124. assert_equal(a[0, ...], a[0])
  125. assert_equal(a[0, ...], a[0,:])
  126. assert_equal(a[..., 0], a[:, 0])
  127. # Slicing with ellipsis always results
  128. # in an array, not a scalar
  129. assert_equal(a[0, ..., 1], np.array(2))
  130. # Assignment with `(Ellipsis,)` on 0-d arrays
  131. b = np.array(1)
  132. b[(Ellipsis,)] = 2
  133. assert_equal(b, 2)
  134. def test_single_int_index(self):
  135. # Single integer index selects one row
  136. a = np.array([[1, 2, 3],
  137. [4, 5, 6],
  138. [7, 8, 9]])
  139. assert_equal(a[0], [1, 2, 3])
  140. assert_equal(a[-1], [7, 8, 9])
  141. # Index out of bounds produces IndexError
  142. assert_raises(IndexError, a.__getitem__, 1 << 30)
  143. # Index overflow produces IndexError
  144. assert_raises(IndexError, a.__getitem__, 1 << 64)
  145. def test_single_bool_index(self):
  146. # Single boolean index
  147. a = np.array([[1, 2, 3],
  148. [4, 5, 6],
  149. [7, 8, 9]])
  150. assert_equal(a[np.array(True)], a[None])
  151. assert_equal(a[np.array(False)], a[None][0:0])
  152. def test_boolean_shape_mismatch(self):
  153. arr = np.ones((5, 4, 3))
  154. index = np.array([True])
  155. assert_raises(IndexError, arr.__getitem__, index)
  156. index = np.array([False] * 6)
  157. assert_raises(IndexError, arr.__getitem__, index)
  158. index = np.zeros((4, 4), dtype=bool)
  159. assert_raises(IndexError, arr.__getitem__, index)
  160. assert_raises(IndexError, arr.__getitem__, (slice(None), index))
  161. def test_boolean_indexing_onedim(self):
  162. # Indexing a 2-dimensional array with
  163. # boolean array of length one
  164. a = np.array([[ 0., 0., 0.]])
  165. b = np.array([ True], dtype=bool)
  166. assert_equal(a[b], a)
  167. # boolean assignment
  168. a[b] = 1.
  169. assert_equal(a, [[1., 1., 1.]])
  170. def test_boolean_assignment_value_mismatch(self):
  171. # A boolean assignment should fail when the shape of the values
  172. # cannot be broadcast to the subscription. (see also gh-3458)
  173. a = np.arange(4)
  174. def f(a, v):
  175. a[a > -1] = v
  176. assert_raises(ValueError, f, a, [])
  177. assert_raises(ValueError, f, a, [1, 2, 3])
  178. assert_raises(ValueError, f, a[:1], [1, 2, 3])
  179. def test_boolean_assignment_needs_api(self):
  180. # See also gh-7666
  181. # This caused a segfault on Python 2 due to the GIL not being
  182. # held when the iterator does not need it, but the transfer function
  183. # does
  184. arr = np.zeros(1000)
  185. indx = np.zeros(1000, dtype=bool)
  186. indx[:100] = True
  187. arr[indx] = np.ones(100, dtype=object)
  188. expected = np.zeros(1000)
  189. expected[:100] = 1
  190. assert_array_equal(arr, expected)
  191. def test_boolean_indexing_twodim(self):
  192. # Indexing a 2-dimensional array with
  193. # 2-dimensional boolean array
  194. a = np.array([[1, 2, 3],
  195. [4, 5, 6],
  196. [7, 8, 9]])
  197. b = np.array([[ True, False, True],
  198. [False, True, False],
  199. [ True, False, True]])
  200. assert_equal(a[b], [1, 3, 5, 7, 9])
  201. assert_equal(a[b[1]], [[4, 5, 6]])
  202. assert_equal(a[b[0]], a[b[2]])
  203. # boolean assignment
  204. a[b] = 0
  205. assert_equal(a, [[0, 2, 0],
  206. [4, 0, 6],
  207. [0, 8, 0]])
  208. def test_boolean_indexing_list(self):
  209. # Regression test for #13715. It's a use-after-free bug which the
  210. # test won't directly catch, but it will show up in valgrind.
  211. a = np.array([1, 2, 3])
  212. b = [True, False, True]
  213. # Two variants of the test because the first takes a fast path
  214. assert_equal(a[b], [1, 3])
  215. assert_equal(a[None, b], [[1, 3]])
  216. def test_reverse_strides_and_subspace_bufferinit(self):
  217. # This tests that the strides are not reversed for simple and
  218. # subspace fancy indexing.
  219. a = np.ones(5)
  220. b = np.zeros(5, dtype=np.intp)[::-1]
  221. c = np.arange(5)[::-1]
  222. a[b] = c
  223. # If the strides are not reversed, the 0 in the arange comes last.
  224. assert_equal(a[0], 0)
  225. # This also tests that the subspace buffer is initialized:
  226. a = np.ones((5, 2))
  227. c = np.arange(10).reshape(5, 2)[::-1]
  228. a[b, :] = c
  229. assert_equal(a[0], [0, 1])
  230. def test_reversed_strides_result_allocation(self):
  231. # Test a bug when calculating the output strides for a result array
  232. # when the subspace size was 1 (and test other cases as well)
  233. a = np.arange(10)[:, None]
  234. i = np.arange(10)[::-1]
  235. assert_array_equal(a[i], a[i.copy('C')])
  236. a = np.arange(20).reshape(-1, 2)
  237. def test_uncontiguous_subspace_assignment(self):
  238. # During development there was a bug activating a skip logic
  239. # based on ndim instead of size.
  240. a = np.full((3, 4, 2), -1)
  241. b = np.full((3, 4, 2), -1)
  242. a[[0, 1]] = np.arange(2 * 4 * 2).reshape(2, 4, 2).T
  243. b[[0, 1]] = np.arange(2 * 4 * 2).reshape(2, 4, 2).T.copy()
  244. assert_equal(a, b)
  245. def test_too_many_fancy_indices_special_case(self):
  246. # Just documents behaviour, this is a small limitation.
  247. a = np.ones((1,) * 32) # 32 is NPY_MAXDIMS
  248. assert_raises(IndexError, a.__getitem__, (np.array([0]),) * 32)
  249. def test_scalar_array_bool(self):
  250. # NumPy bools can be used as boolean index (python ones as of yet not)
  251. a = np.array(1)
  252. assert_equal(a[np.bool_(True)], a[np.array(True)])
  253. assert_equal(a[np.bool_(False)], a[np.array(False)])
  254. # After deprecating bools as integers:
  255. #a = np.array([0,1,2])
  256. #assert_equal(a[True, :], a[None, :])
  257. #assert_equal(a[:, True], a[:, None])
  258. #
  259. #assert_(not np.may_share_memory(a, a[True, :]))
  260. def test_everything_returns_views(self):
  261. # Before `...` would return a itself.
  262. a = np.arange(5)
  263. assert_(a is not a[()])
  264. assert_(a is not a[...])
  265. assert_(a is not a[:])
  266. def test_broaderrors_indexing(self):
  267. a = np.zeros((5, 5))
  268. assert_raises(IndexError, a.__getitem__, ([0, 1], [0, 1, 2]))
  269. assert_raises(IndexError, a.__setitem__, ([0, 1], [0, 1, 2]), 0)
  270. def test_trivial_fancy_out_of_bounds(self):
  271. a = np.zeros(5)
  272. ind = np.ones(20, dtype=np.intp)
  273. ind[-1] = 10
  274. assert_raises(IndexError, a.__getitem__, ind)
  275. assert_raises(IndexError, a.__setitem__, ind, 0)
  276. ind = np.ones(20, dtype=np.intp)
  277. ind[0] = 11
  278. assert_raises(IndexError, a.__getitem__, ind)
  279. assert_raises(IndexError, a.__setitem__, ind, 0)
  280. def test_trivial_fancy_not_possible(self):
  281. # Test that the fast path for trivial assignment is not incorrectly
  282. # used when the index is not contiguous or 1D, see also gh-11467.
  283. a = np.arange(6)
  284. idx = np.arange(6, dtype=np.intp).reshape(2, 1, 3)[:, :, 0]
  285. assert_array_equal(a[idx], idx)
  286. # this case must not go into the fast path, note that idx is
  287. # a non-contiuguous none 1D array here.
  288. a[idx] = -1
  289. res = np.arange(6)
  290. res[0] = -1
  291. res[3] = -1
  292. assert_array_equal(a, res)
  293. def test_nonbaseclass_values(self):
  294. class SubClass(np.ndarray):
  295. def __array_finalize__(self, old):
  296. # Have array finalize do funny things
  297. self.fill(99)
  298. a = np.zeros((5, 5))
  299. s = a.copy().view(type=SubClass)
  300. s.fill(1)
  301. a[[0, 1, 2, 3, 4], :] = s
  302. assert_((a == 1).all())
  303. # Subspace is last, so transposing might want to finalize
  304. a[:, [0, 1, 2, 3, 4]] = s
  305. assert_((a == 1).all())
  306. a.fill(0)
  307. a[...] = s
  308. assert_((a == 1).all())
  309. def test_array_like_values(self):
  310. # Similar to the above test, but use a memoryview instead
  311. a = np.zeros((5, 5))
  312. s = np.arange(25, dtype=np.float64).reshape(5, 5)
  313. a[[0, 1, 2, 3, 4], :] = memoryview(s)
  314. assert_array_equal(a, s)
  315. a[:, [0, 1, 2, 3, 4]] = memoryview(s)
  316. assert_array_equal(a, s)
  317. a[...] = memoryview(s)
  318. assert_array_equal(a, s)
  319. def test_subclass_writeable(self):
  320. d = np.rec.array([('NGC1001', 11), ('NGC1002', 1.), ('NGC1003', 1.)],
  321. dtype=[('target', 'S20'), ('V_mag', '>f4')])
  322. ind = np.array([False, True, True], dtype=bool)
  323. assert_(d[ind].flags.writeable)
  324. ind = np.array([0, 1])
  325. assert_(d[ind].flags.writeable)
  326. assert_(d[...].flags.writeable)
  327. assert_(d[0].flags.writeable)
  328. def test_memory_order(self):
  329. # This is not necessary to preserve. Memory layouts for
  330. # more complex indices are not as simple.
  331. a = np.arange(10)
  332. b = np.arange(10).reshape(5,2).T
  333. assert_(a[b].flags.f_contiguous)
  334. # Takes a different implementation branch:
  335. a = a.reshape(-1, 1)
  336. assert_(a[b, 0].flags.f_contiguous)
  337. def test_scalar_return_type(self):
  338. # Full scalar indices should return scalars and object
  339. # arrays should not call PyArray_Return on their items
  340. class Zero:
  341. # The most basic valid indexing
  342. def __index__(self):
  343. return 0
  344. z = Zero()
  345. class ArrayLike:
  346. # Simple array, should behave like the array
  347. def __array__(self):
  348. return np.array(0)
  349. a = np.zeros(())
  350. assert_(isinstance(a[()], np.float_))
  351. a = np.zeros(1)
  352. assert_(isinstance(a[z], np.float_))
  353. a = np.zeros((1, 1))
  354. assert_(isinstance(a[z, np.array(0)], np.float_))
  355. assert_(isinstance(a[z, ArrayLike()], np.float_))
  356. # And object arrays do not call it too often:
  357. b = np.array(0)
  358. a = np.array(0, dtype=object)
  359. a[()] = b
  360. assert_(isinstance(a[()], np.ndarray))
  361. a = np.array([b, None])
  362. assert_(isinstance(a[z], np.ndarray))
  363. a = np.array([[b, None]])
  364. assert_(isinstance(a[z, np.array(0)], np.ndarray))
  365. assert_(isinstance(a[z, ArrayLike()], np.ndarray))
  366. def test_small_regressions(self):
  367. # Reference count of intp for index checks
  368. a = np.array([0])
  369. if HAS_REFCOUNT:
  370. refcount = sys.getrefcount(np.dtype(np.intp))
  371. # item setting always checks indices in separate function:
  372. a[np.array([0], dtype=np.intp)] = 1
  373. a[np.array([0], dtype=np.uint8)] = 1
  374. assert_raises(IndexError, a.__setitem__,
  375. np.array([1], dtype=np.intp), 1)
  376. assert_raises(IndexError, a.__setitem__,
  377. np.array([1], dtype=np.uint8), 1)
  378. if HAS_REFCOUNT:
  379. assert_equal(sys.getrefcount(np.dtype(np.intp)), refcount)
  380. def test_unaligned(self):
  381. v = (np.zeros(64, dtype=np.int8) + ord('a'))[1:-7]
  382. d = v.view(np.dtype("S8"))
  383. # unaligned source
  384. x = (np.zeros(16, dtype=np.int8) + ord('a'))[1:-7]
  385. x = x.view(np.dtype("S8"))
  386. x[...] = np.array("b" * 8, dtype="S")
  387. b = np.arange(d.size)
  388. #trivial
  389. assert_equal(d[b], d)
  390. d[b] = x
  391. # nontrivial
  392. # unaligned index array
  393. b = np.zeros(d.size + 1).view(np.int8)[1:-(np.intp(0).itemsize - 1)]
  394. b = b.view(np.intp)[:d.size]
  395. b[...] = np.arange(d.size)
  396. assert_equal(d[b.astype(np.int16)], d)
  397. d[b.astype(np.int16)] = x
  398. # boolean
  399. d[b % 2 == 0]
  400. d[b % 2 == 0] = x[::2]
  401. def test_tuple_subclass(self):
  402. arr = np.ones((5, 5))
  403. # A tuple subclass should also be an nd-index
  404. class TupleSubclass(tuple):
  405. pass
  406. index = ([1], [1])
  407. index = TupleSubclass(index)
  408. assert_(arr[index].shape == (1,))
  409. # Unlike the non nd-index:
  410. assert_(arr[index,].shape != (1,))
  411. def test_broken_sequence_not_nd_index(self):
  412. # See gh-5063:
  413. # If we have an object which claims to be a sequence, but fails
  414. # on item getting, this should not be converted to an nd-index (tuple)
  415. # If this object happens to be a valid index otherwise, it should work
  416. # This object here is very dubious and probably bad though:
  417. class SequenceLike:
  418. def __index__(self):
  419. return 0
  420. def __len__(self):
  421. return 1
  422. def __getitem__(self, item):
  423. raise IndexError('Not possible')
  424. arr = np.arange(10)
  425. assert_array_equal(arr[SequenceLike()], arr[SequenceLike(),])
  426. # also test that field indexing does not segfault
  427. # for a similar reason, by indexing a structured array
  428. arr = np.zeros((1,), dtype=[('f1', 'i8'), ('f2', 'i8')])
  429. assert_array_equal(arr[SequenceLike()], arr[SequenceLike(),])
  430. def test_indexing_array_weird_strides(self):
  431. # See also gh-6221
  432. # the shapes used here come from the issue and create the correct
  433. # size for the iterator buffering size.
  434. x = np.ones(10)
  435. x2 = np.ones((10, 2))
  436. ind = np.arange(10)[:, None, None, None]
  437. ind = np.broadcast_to(ind, (10, 55, 4, 4))
  438. # single advanced index case
  439. assert_array_equal(x[ind], x[ind.copy()])
  440. # higher dimensional advanced index
  441. zind = np.zeros(4, dtype=np.intp)
  442. assert_array_equal(x2[ind, zind], x2[ind.copy(), zind])
  443. def test_indexing_array_negative_strides(self):
  444. # From gh-8264,
  445. # core dumps if negative strides are used in iteration
  446. arro = np.zeros((4, 4))
  447. arr = arro[::-1, ::-1]
  448. slices = (slice(None), [0, 1, 2, 3])
  449. arr[slices] = 10
  450. assert_array_equal(arr, 10.)
  451. def test_character_assignment(self):
  452. # This is an example a function going through CopyObject which
  453. # used to have an untested special path for scalars
  454. # (the character special dtype case, should be deprecated probably)
  455. arr = np.zeros((1, 5), dtype="c")
  456. arr[0] = np.str_("asdfg") # must assign as a sequence
  457. assert_array_equal(arr[0], np.array("asdfg", dtype="c"))
  458. assert arr[0, 1] == b"s" # make sure not all were set to "a" for both
  459. @pytest.mark.parametrize("index",
  460. [True, False, np.array([0])])
  461. @pytest.mark.parametrize("num", [32, 40])
  462. @pytest.mark.parametrize("original_ndim", [1, 32])
  463. def test_too_many_advanced_indices(self, index, num, original_ndim):
  464. # These are limitations based on the number of arguments we can process.
  465. # For `num=32` (and all boolean cases), the result is actually define;
  466. # but the use of NpyIter (NPY_MAXARGS) limits it for technical reasons.
  467. arr = np.ones((1,) * original_ndim)
  468. with pytest.raises(IndexError):
  469. arr[(index,) * num]
  470. with pytest.raises(IndexError):
  471. arr[(index,) * num] = 1.
  472. @pytest.mark.skipif(IS_WASM, reason="no threading")
  473. def test_structured_advanced_indexing(self):
  474. # Test that copyswap(n) used by integer array indexing is threadsafe
  475. # for structured datatypes, see gh-15387. This test can behave randomly.
  476. from concurrent.futures import ThreadPoolExecutor
  477. # Create a deeply nested dtype to make a failure more likely:
  478. dt = np.dtype([("", "f8")])
  479. dt = np.dtype([("", dt)] * 2)
  480. dt = np.dtype([("", dt)] * 2)
  481. # The array should be large enough to likely run into threading issues
  482. arr = np.random.uniform(size=(6000, 8)).view(dt)[:, 0]
  483. rng = np.random.default_rng()
  484. def func(arr):
  485. indx = rng.integers(0, len(arr), size=6000, dtype=np.intp)
  486. arr[indx]
  487. tpe = ThreadPoolExecutor(max_workers=8)
  488. futures = [tpe.submit(func, arr) for _ in range(10)]
  489. for f in futures:
  490. f.result()
  491. assert arr.dtype is dt
  492. def test_nontuple_ndindex(self):
  493. a = np.arange(25).reshape((5, 5))
  494. assert_equal(a[[0, 1]], np.array([a[0], a[1]]))
  495. assert_equal(a[[0, 1], [0, 1]], np.array([0, 6]))
  496. assert_raises(IndexError, a.__getitem__, [slice(None)])
  497. class TestFieldIndexing:
  498. def test_scalar_return_type(self):
  499. # Field access on an array should return an array, even if it
  500. # is 0-d.
  501. a = np.zeros((), [('a','f8')])
  502. assert_(isinstance(a['a'], np.ndarray))
  503. assert_(isinstance(a[['a']], np.ndarray))
  504. class TestBroadcastedAssignments:
  505. def assign(self, a, ind, val):
  506. a[ind] = val
  507. return a
  508. def test_prepending_ones(self):
  509. a = np.zeros((3, 2))
  510. a[...] = np.ones((1, 3, 2))
  511. # Fancy with subspace with and without transpose
  512. a[[0, 1, 2], :] = np.ones((1, 3, 2))
  513. a[:, [0, 1]] = np.ones((1, 3, 2))
  514. # Fancy without subspace (with broadcasting)
  515. a[[[0], [1], [2]], [0, 1]] = np.ones((1, 3, 2))
  516. def test_prepend_not_one(self):
  517. assign = self.assign
  518. s_ = np.s_
  519. a = np.zeros(5)
  520. # Too large and not only ones.
  521. assert_raises(ValueError, assign, a, s_[...], np.ones((2, 1)))
  522. assert_raises(ValueError, assign, a, s_[[1, 2, 3],], np.ones((2, 1)))
  523. assert_raises(ValueError, assign, a, s_[[[1], [2]],], np.ones((2,2,1)))
  524. def test_simple_broadcasting_errors(self):
  525. assign = self.assign
  526. s_ = np.s_
  527. a = np.zeros((5, 1))
  528. assert_raises(ValueError, assign, a, s_[...], np.zeros((5, 2)))
  529. assert_raises(ValueError, assign, a, s_[...], np.zeros((5, 0)))
  530. assert_raises(ValueError, assign, a, s_[:, [0]], np.zeros((5, 2)))
  531. assert_raises(ValueError, assign, a, s_[:, [0]], np.zeros((5, 0)))
  532. assert_raises(ValueError, assign, a, s_[[0], :], np.zeros((2, 1)))
  533. @pytest.mark.parametrize("index", [
  534. (..., [1, 2], slice(None)),
  535. ([0, 1], ..., 0),
  536. (..., [1, 2], [1, 2])])
  537. def test_broadcast_error_reports_correct_shape(self, index):
  538. values = np.zeros((100, 100)) # will never broadcast below
  539. arr = np.zeros((3, 4, 5, 6, 7))
  540. # We currently report without any spaces (could be changed)
  541. shape_str = str(arr[index].shape).replace(" ", "")
  542. with pytest.raises(ValueError) as e:
  543. arr[index] = values
  544. assert str(e.value).endswith(shape_str)
  545. def test_index_is_larger(self):
  546. # Simple case of fancy index broadcasting of the index.
  547. a = np.zeros((5, 5))
  548. a[[[0], [1], [2]], [0, 1, 2]] = [2, 3, 4]
  549. assert_((a[:3, :3] == [2, 3, 4]).all())
  550. def test_broadcast_subspace(self):
  551. a = np.zeros((100, 100))
  552. v = np.arange(100)[:,None]
  553. b = np.arange(100)[::-1]
  554. a[b] = v
  555. assert_((a[::-1] == v).all())
  556. class TestSubclasses:
  557. def test_basic(self):
  558. # Test that indexing in various ways produces SubClass instances,
  559. # and that the base is set up correctly: the original subclass
  560. # instance for views, and a new ndarray for advanced/boolean indexing
  561. # where a copy was made (latter a regression test for gh-11983).
  562. class SubClass(np.ndarray):
  563. pass
  564. a = np.arange(5)
  565. s = a.view(SubClass)
  566. s_slice = s[:3]
  567. assert_(type(s_slice) is SubClass)
  568. assert_(s_slice.base is s)
  569. assert_array_equal(s_slice, a[:3])
  570. s_fancy = s[[0, 1, 2]]
  571. assert_(type(s_fancy) is SubClass)
  572. assert_(s_fancy.base is not s)
  573. assert_(type(s_fancy.base) is np.ndarray)
  574. assert_array_equal(s_fancy, a[[0, 1, 2]])
  575. assert_array_equal(s_fancy.base, a[[0, 1, 2]])
  576. s_bool = s[s > 0]
  577. assert_(type(s_bool) is SubClass)
  578. assert_(s_bool.base is not s)
  579. assert_(type(s_bool.base) is np.ndarray)
  580. assert_array_equal(s_bool, a[a > 0])
  581. assert_array_equal(s_bool.base, a[a > 0])
  582. def test_fancy_on_read_only(self):
  583. # Test that fancy indexing on read-only SubClass does not make a
  584. # read-only copy (gh-14132)
  585. class SubClass(np.ndarray):
  586. pass
  587. a = np.arange(5)
  588. s = a.view(SubClass)
  589. s.flags.writeable = False
  590. s_fancy = s[[0, 1, 2]]
  591. assert_(s_fancy.flags.writeable)
  592. def test_finalize_gets_full_info(self):
  593. # Array finalize should be called on the filled array.
  594. class SubClass(np.ndarray):
  595. def __array_finalize__(self, old):
  596. self.finalize_status = np.array(self)
  597. self.old = old
  598. s = np.arange(10).view(SubClass)
  599. new_s = s[:3]
  600. assert_array_equal(new_s.finalize_status, new_s)
  601. assert_array_equal(new_s.old, s)
  602. new_s = s[[0,1,2,3]]
  603. assert_array_equal(new_s.finalize_status, new_s)
  604. assert_array_equal(new_s.old, s)
  605. new_s = s[s > 0]
  606. assert_array_equal(new_s.finalize_status, new_s)
  607. assert_array_equal(new_s.old, s)
  608. class TestFancyIndexingCast:
  609. def test_boolean_index_cast_assign(self):
  610. # Setup the boolean index and float arrays.
  611. shape = (8, 63)
  612. bool_index = np.zeros(shape).astype(bool)
  613. bool_index[0, 1] = True
  614. zero_array = np.zeros(shape)
  615. # Assigning float is fine.
  616. zero_array[bool_index] = np.array([1])
  617. assert_equal(zero_array[0, 1], 1)
  618. # Fancy indexing works, although we get a cast warning.
  619. assert_warns(np.ComplexWarning,
  620. zero_array.__setitem__, ([0], [1]), np.array([2 + 1j]))
  621. assert_equal(zero_array[0, 1], 2) # No complex part
  622. # Cast complex to float, throwing away the imaginary portion.
  623. assert_warns(np.ComplexWarning,
  624. zero_array.__setitem__, bool_index, np.array([1j]))
  625. assert_equal(zero_array[0, 1], 0)
  626. class TestFancyIndexingEquivalence:
  627. def test_object_assign(self):
  628. # Check that the field and object special case using copyto is active.
  629. # The right hand side cannot be converted to an array here.
  630. a = np.arange(5, dtype=object)
  631. b = a.copy()
  632. a[:3] = [1, (1,2), 3]
  633. b[[0, 1, 2]] = [1, (1,2), 3]
  634. assert_array_equal(a, b)
  635. # test same for subspace fancy indexing
  636. b = np.arange(5, dtype=object)[None, :]
  637. b[[0], :3] = [[1, (1,2), 3]]
  638. assert_array_equal(a, b[0])
  639. # Check that swapping of axes works.
  640. # There was a bug that made the later assignment throw a ValueError
  641. # do to an incorrectly transposed temporary right hand side (gh-5714)
  642. b = b.T
  643. b[:3, [0]] = [[1], [(1,2)], [3]]
  644. assert_array_equal(a, b[:, 0])
  645. # Another test for the memory order of the subspace
  646. arr = np.ones((3, 4, 5), dtype=object)
  647. # Equivalent slicing assignment for comparison
  648. cmp_arr = arr.copy()
  649. cmp_arr[:1, ...] = [[[1], [2], [3], [4]]]
  650. arr[[0], ...] = [[[1], [2], [3], [4]]]
  651. assert_array_equal(arr, cmp_arr)
  652. arr = arr.copy('F')
  653. arr[[0], ...] = [[[1], [2], [3], [4]]]
  654. assert_array_equal(arr, cmp_arr)
  655. def test_cast_equivalence(self):
  656. # Yes, normal slicing uses unsafe casting.
  657. a = np.arange(5)
  658. b = a.copy()
  659. a[:3] = np.array(['2', '-3', '-1'])
  660. b[[0, 2, 1]] = np.array(['2', '-1', '-3'])
  661. assert_array_equal(a, b)
  662. # test the same for subspace fancy indexing
  663. b = np.arange(5)[None, :]
  664. b[[0], :3] = np.array([['2', '-3', '-1']])
  665. assert_array_equal(a, b[0])
  666. class TestMultiIndexingAutomated:
  667. """
  668. These tests use code to mimic the C-Code indexing for selection.
  669. NOTE:
  670. * This still lacks tests for complex item setting.
  671. * If you change behavior of indexing, you might want to modify
  672. these tests to try more combinations.
  673. * Behavior was written to match numpy version 1.8. (though a
  674. first version matched 1.7.)
  675. * Only tuple indices are supported by the mimicking code.
  676. (and tested as of writing this)
  677. * Error types should match most of the time as long as there
  678. is only one error. For multiple errors, what gets raised
  679. will usually not be the same one. They are *not* tested.
  680. Update 2016-11-30: It is probably not worth maintaining this test
  681. indefinitely and it can be dropped if maintenance becomes a burden.
  682. """
  683. def setup_method(self):
  684. self.a = np.arange(np.prod([3, 1, 5, 6])).reshape(3, 1, 5, 6)
  685. self.b = np.empty((3, 0, 5, 6))
  686. self.complex_indices = ['skip', Ellipsis,
  687. 0,
  688. # Boolean indices, up to 3-d for some special cases of eating up
  689. # dimensions, also need to test all False
  690. np.array([True, False, False]),
  691. np.array([[True, False], [False, True]]),
  692. np.array([[[False, False], [False, False]]]),
  693. # Some slices:
  694. slice(-5, 5, 2),
  695. slice(1, 1, 100),
  696. slice(4, -1, -2),
  697. slice(None, None, -3),
  698. # Some Fancy indexes:
  699. np.empty((0, 1, 1), dtype=np.intp), # empty and can be broadcast
  700. np.array([0, 1, -2]),
  701. np.array([[2], [0], [1]]),
  702. np.array([[0, -1], [0, 1]], dtype=np.dtype('intp').newbyteorder()),
  703. np.array([2, -1], dtype=np.int8),
  704. np.zeros([1]*31, dtype=int), # trigger too large array.
  705. np.array([0., 1.])] # invalid datatype
  706. # Some simpler indices that still cover a bit more
  707. self.simple_indices = [Ellipsis, None, -1, [1], np.array([True]),
  708. 'skip']
  709. # Very simple ones to fill the rest:
  710. self.fill_indices = [slice(None, None), 0]
  711. def _get_multi_index(self, arr, indices):
  712. """Mimic multi dimensional indexing.
  713. Parameters
  714. ----------
  715. arr : ndarray
  716. Array to be indexed.
  717. indices : tuple of index objects
  718. Returns
  719. -------
  720. out : ndarray
  721. An array equivalent to the indexing operation (but always a copy).
  722. `arr[indices]` should be identical.
  723. no_copy : bool
  724. Whether the indexing operation requires a copy. If this is `True`,
  725. `np.may_share_memory(arr, arr[indices])` should be `True` (with
  726. some exceptions for scalars and possibly 0-d arrays).
  727. Notes
  728. -----
  729. While the function may mostly match the errors of normal indexing this
  730. is generally not the case.
  731. """
  732. in_indices = list(indices)
  733. indices = []
  734. # if False, this is a fancy or boolean index
  735. no_copy = True
  736. # number of fancy/scalar indexes that are not consecutive
  737. num_fancy = 0
  738. # number of dimensions indexed by a "fancy" index
  739. fancy_dim = 0
  740. # NOTE: This is a funny twist (and probably OK to change).
  741. # The boolean array has illegal indexes, but this is
  742. # allowed if the broadcast fancy-indices are 0-sized.
  743. # This variable is to catch that case.
  744. error_unless_broadcast_to_empty = False
  745. # We need to handle Ellipsis and make arrays from indices, also
  746. # check if this is fancy indexing (set no_copy).
  747. ndim = 0
  748. ellipsis_pos = None # define here mostly to replace all but first.
  749. for i, indx in enumerate(in_indices):
  750. if indx is None:
  751. continue
  752. if isinstance(indx, np.ndarray) and indx.dtype == bool:
  753. no_copy = False
  754. if indx.ndim == 0:
  755. raise IndexError
  756. # boolean indices can have higher dimensions
  757. ndim += indx.ndim
  758. fancy_dim += indx.ndim
  759. continue
  760. if indx is Ellipsis:
  761. if ellipsis_pos is None:
  762. ellipsis_pos = i
  763. continue # do not increment ndim counter
  764. raise IndexError
  765. if isinstance(indx, slice):
  766. ndim += 1
  767. continue
  768. if not isinstance(indx, np.ndarray):
  769. # This could be open for changes in numpy.
  770. # numpy should maybe raise an error if casting to intp
  771. # is not safe. It rejects np.array([1., 2.]) but not
  772. # [1., 2.] as index (same for ie. np.take).
  773. # (Note the importance of empty lists if changing this here)
  774. try:
  775. indx = np.array(indx, dtype=np.intp)
  776. except ValueError:
  777. raise IndexError
  778. in_indices[i] = indx
  779. elif indx.dtype.kind != 'b' and indx.dtype.kind != 'i':
  780. raise IndexError('arrays used as indices must be of '
  781. 'integer (or boolean) type')
  782. if indx.ndim != 0:
  783. no_copy = False
  784. ndim += 1
  785. fancy_dim += 1
  786. if arr.ndim - ndim < 0:
  787. # we can't take more dimensions then we have, not even for 0-d
  788. # arrays. since a[()] makes sense, but not a[(),]. We will
  789. # raise an error later on, unless a broadcasting error occurs
  790. # first.
  791. raise IndexError
  792. if ndim == 0 and None not in in_indices:
  793. # Well we have no indexes or one Ellipsis. This is legal.
  794. return arr.copy(), no_copy
  795. if ellipsis_pos is not None:
  796. in_indices[ellipsis_pos:ellipsis_pos+1] = ([slice(None, None)] *
  797. (arr.ndim - ndim))
  798. for ax, indx in enumerate(in_indices):
  799. if isinstance(indx, slice):
  800. # convert to an index array
  801. indx = np.arange(*indx.indices(arr.shape[ax]))
  802. indices.append(['s', indx])
  803. continue
  804. elif indx is None:
  805. # this is like taking a slice with one element from a new axis:
  806. indices.append(['n', np.array([0], dtype=np.intp)])
  807. arr = arr.reshape((arr.shape[:ax] + (1,) + arr.shape[ax:]))
  808. continue
  809. if isinstance(indx, np.ndarray) and indx.dtype == bool:
  810. if indx.shape != arr.shape[ax:ax+indx.ndim]:
  811. raise IndexError
  812. try:
  813. flat_indx = np.ravel_multi_index(np.nonzero(indx),
  814. arr.shape[ax:ax+indx.ndim], mode='raise')
  815. except Exception:
  816. error_unless_broadcast_to_empty = True
  817. # fill with 0s instead, and raise error later
  818. flat_indx = np.array([0]*indx.sum(), dtype=np.intp)
  819. # concatenate axis into a single one:
  820. if indx.ndim != 0:
  821. arr = arr.reshape((arr.shape[:ax]
  822. + (np.prod(arr.shape[ax:ax+indx.ndim]),)
  823. + arr.shape[ax+indx.ndim:]))
  824. indx = flat_indx
  825. else:
  826. # This could be changed, a 0-d boolean index can
  827. # make sense (even outside the 0-d indexed array case)
  828. # Note that originally this is could be interpreted as
  829. # integer in the full integer special case.
  830. raise IndexError
  831. else:
  832. # If the index is a singleton, the bounds check is done
  833. # before the broadcasting. This used to be different in <1.9
  834. if indx.ndim == 0:
  835. if indx >= arr.shape[ax] or indx < -arr.shape[ax]:
  836. raise IndexError
  837. if indx.ndim == 0:
  838. # The index is a scalar. This used to be two fold, but if
  839. # fancy indexing was active, the check was done later,
  840. # possibly after broadcasting it away (1.7. or earlier).
  841. # Now it is always done.
  842. if indx >= arr.shape[ax] or indx < - arr.shape[ax]:
  843. raise IndexError
  844. if (len(indices) > 0 and
  845. indices[-1][0] == 'f' and
  846. ax != ellipsis_pos):
  847. # NOTE: There could still have been a 0-sized Ellipsis
  848. # between them. Checked that with ellipsis_pos.
  849. indices[-1].append(indx)
  850. else:
  851. # We have a fancy index that is not after an existing one.
  852. # NOTE: A 0-d array triggers this as well, while one may
  853. # expect it to not trigger it, since a scalar would not be
  854. # considered fancy indexing.
  855. num_fancy += 1
  856. indices.append(['f', indx])
  857. if num_fancy > 1 and not no_copy:
  858. # We have to flush the fancy indexes left
  859. new_indices = indices[:]
  860. axes = list(range(arr.ndim))
  861. fancy_axes = []
  862. new_indices.insert(0, ['f'])
  863. ni = 0
  864. ai = 0
  865. for indx in indices:
  866. ni += 1
  867. if indx[0] == 'f':
  868. new_indices[0].extend(indx[1:])
  869. del new_indices[ni]
  870. ni -= 1
  871. for ax in range(ai, ai + len(indx[1:])):
  872. fancy_axes.append(ax)
  873. axes.remove(ax)
  874. ai += len(indx) - 1 # axis we are at
  875. indices = new_indices
  876. # and now we need to transpose arr:
  877. arr = arr.transpose(*(fancy_axes + axes))
  878. # We only have one 'f' index now and arr is transposed accordingly.
  879. # Now handle newaxis by reshaping...
  880. ax = 0
  881. for indx in indices:
  882. if indx[0] == 'f':
  883. if len(indx) == 1:
  884. continue
  885. # First of all, reshape arr to combine fancy axes into one:
  886. orig_shape = arr.shape
  887. orig_slice = orig_shape[ax:ax + len(indx[1:])]
  888. arr = arr.reshape((arr.shape[:ax]
  889. + (np.prod(orig_slice).astype(int),)
  890. + arr.shape[ax + len(indx[1:]):]))
  891. # Check if broadcasting works
  892. res = np.broadcast(*indx[1:])
  893. # unfortunately the indices might be out of bounds. So check
  894. # that first, and use mode='wrap' then. However only if
  895. # there are any indices...
  896. if res.size != 0:
  897. if error_unless_broadcast_to_empty:
  898. raise IndexError
  899. for _indx, _size in zip(indx[1:], orig_slice):
  900. if _indx.size == 0:
  901. continue
  902. if np.any(_indx >= _size) or np.any(_indx < -_size):
  903. raise IndexError
  904. if len(indx[1:]) == len(orig_slice):
  905. if np.product(orig_slice) == 0:
  906. # Work around for a crash or IndexError with 'wrap'
  907. # in some 0-sized cases.
  908. try:
  909. mi = np.ravel_multi_index(indx[1:], orig_slice,
  910. mode='raise')
  911. except Exception:
  912. # This happens with 0-sized orig_slice (sometimes?)
  913. # here it is a ValueError, but indexing gives a:
  914. raise IndexError('invalid index into 0-sized')
  915. else:
  916. mi = np.ravel_multi_index(indx[1:], orig_slice,
  917. mode='wrap')
  918. else:
  919. # Maybe never happens...
  920. raise ValueError
  921. arr = arr.take(mi.ravel(), axis=ax)
  922. try:
  923. arr = arr.reshape((arr.shape[:ax]
  924. + mi.shape
  925. + arr.shape[ax+1:]))
  926. except ValueError:
  927. # too many dimensions, probably
  928. raise IndexError
  929. ax += mi.ndim
  930. continue
  931. # If we are here, we have a 1D array for take:
  932. arr = arr.take(indx[1], axis=ax)
  933. ax += 1
  934. return arr, no_copy
  935. def _check_multi_index(self, arr, index):
  936. """Check a multi index item getting and simple setting.
  937. Parameters
  938. ----------
  939. arr : ndarray
  940. Array to be indexed, must be a reshaped arange.
  941. index : tuple of indexing objects
  942. Index being tested.
  943. """
  944. # Test item getting
  945. try:
  946. mimic_get, no_copy = self._get_multi_index(arr, index)
  947. except Exception as e:
  948. if HAS_REFCOUNT:
  949. prev_refcount = sys.getrefcount(arr)
  950. assert_raises(type(e), arr.__getitem__, index)
  951. assert_raises(type(e), arr.__setitem__, index, 0)
  952. if HAS_REFCOUNT:
  953. assert_equal(prev_refcount, sys.getrefcount(arr))
  954. return
  955. self._compare_index_result(arr, index, mimic_get, no_copy)
  956. def _check_single_index(self, arr, index):
  957. """Check a single index item getting and simple setting.
  958. Parameters
  959. ----------
  960. arr : ndarray
  961. Array to be indexed, must be an arange.
  962. index : indexing object
  963. Index being tested. Must be a single index and not a tuple
  964. of indexing objects (see also `_check_multi_index`).
  965. """
  966. try:
  967. mimic_get, no_copy = self._get_multi_index(arr, (index,))
  968. except Exception as e:
  969. if HAS_REFCOUNT:
  970. prev_refcount = sys.getrefcount(arr)
  971. assert_raises(type(e), arr.__getitem__, index)
  972. assert_raises(type(e), arr.__setitem__, index, 0)
  973. if HAS_REFCOUNT:
  974. assert_equal(prev_refcount, sys.getrefcount(arr))
  975. return
  976. self._compare_index_result(arr, index, mimic_get, no_copy)
  977. def _compare_index_result(self, arr, index, mimic_get, no_copy):
  978. """Compare mimicked result to indexing result.
  979. """
  980. arr = arr.copy()
  981. indexed_arr = arr[index]
  982. assert_array_equal(indexed_arr, mimic_get)
  983. # Check if we got a view, unless its a 0-sized or 0-d array.
  984. # (then its not a view, and that does not matter)
  985. if indexed_arr.size != 0 and indexed_arr.ndim != 0:
  986. assert_(np.may_share_memory(indexed_arr, arr) == no_copy)
  987. # Check reference count of the original array
  988. if HAS_REFCOUNT:
  989. if no_copy:
  990. # refcount increases by one:
  991. assert_equal(sys.getrefcount(arr), 3)
  992. else:
  993. assert_equal(sys.getrefcount(arr), 2)
  994. # Test non-broadcast setitem:
  995. b = arr.copy()
  996. b[index] = mimic_get + 1000
  997. if b.size == 0:
  998. return # nothing to compare here...
  999. if no_copy and indexed_arr.ndim != 0:
  1000. # change indexed_arr in-place to manipulate original:
  1001. indexed_arr += 1000
  1002. assert_array_equal(arr, b)
  1003. return
  1004. # Use the fact that the array is originally an arange:
  1005. arr.flat[indexed_arr.ravel()] += 1000
  1006. assert_array_equal(arr, b)
  1007. def test_boolean(self):
  1008. a = np.array(5)
  1009. assert_equal(a[np.array(True)], 5)
  1010. a[np.array(True)] = 1
  1011. assert_equal(a, 1)
  1012. # NOTE: This is different from normal broadcasting, as
  1013. # arr[boolean_array] works like in a multi index. Which means
  1014. # it is aligned to the left. This is probably correct for
  1015. # consistency with arr[boolean_array,] also no broadcasting
  1016. # is done at all
  1017. self._check_multi_index(
  1018. self.a, (np.zeros_like(self.a, dtype=bool),))
  1019. self._check_multi_index(
  1020. self.a, (np.zeros_like(self.a, dtype=bool)[..., 0],))
  1021. self._check_multi_index(
  1022. self.a, (np.zeros_like(self.a, dtype=bool)[None, ...],))
  1023. def test_multidim(self):
  1024. # Automatically test combinations with complex indexes on 2nd (or 1st)
  1025. # spot and the simple ones in one other spot.
  1026. with warnings.catch_warnings():
  1027. # This is so that np.array(True) is not accepted in a full integer
  1028. # index, when running the file separately.
  1029. warnings.filterwarnings('error', '', DeprecationWarning)
  1030. warnings.filterwarnings('error', '', np.VisibleDeprecationWarning)
  1031. def isskip(idx):
  1032. return isinstance(idx, str) and idx == "skip"
  1033. for simple_pos in [0, 2, 3]:
  1034. tocheck = [self.fill_indices, self.complex_indices,
  1035. self.fill_indices, self.fill_indices]
  1036. tocheck[simple_pos] = self.simple_indices
  1037. for index in product(*tocheck):
  1038. index = tuple(i for i in index if not isskip(i))
  1039. self._check_multi_index(self.a, index)
  1040. self._check_multi_index(self.b, index)
  1041. # Check very simple item getting:
  1042. self._check_multi_index(self.a, (0, 0, 0, 0))
  1043. self._check_multi_index(self.b, (0, 0, 0, 0))
  1044. # Also check (simple cases of) too many indices:
  1045. assert_raises(IndexError, self.a.__getitem__, (0, 0, 0, 0, 0))
  1046. assert_raises(IndexError, self.a.__setitem__, (0, 0, 0, 0, 0), 0)
  1047. assert_raises(IndexError, self.a.__getitem__, (0, 0, [1], 0, 0))
  1048. assert_raises(IndexError, self.a.__setitem__, (0, 0, [1], 0, 0), 0)
  1049. def test_1d(self):
  1050. a = np.arange(10)
  1051. for index in self.complex_indices:
  1052. self._check_single_index(a, index)
  1053. class TestFloatNonIntegerArgument:
  1054. """
  1055. These test that ``TypeError`` is raised when you try to use
  1056. non-integers as arguments to for indexing and slicing e.g. ``a[0.0:5]``
  1057. and ``a[0.5]``, or other functions like ``array.reshape(1., -1)``.
  1058. """
  1059. def test_valid_indexing(self):
  1060. # These should raise no errors.
  1061. a = np.array([[[5]]])
  1062. a[np.array([0])]
  1063. a[[0, 0]]
  1064. a[:, [0, 0]]
  1065. a[:, 0,:]
  1066. a[:,:,:]
  1067. def test_valid_slicing(self):
  1068. # These should raise no errors.
  1069. a = np.array([[[5]]])
  1070. a[::]
  1071. a[0:]
  1072. a[:2]
  1073. a[0:2]
  1074. a[::2]
  1075. a[1::2]
  1076. a[:2:2]
  1077. a[1:2:2]
  1078. def test_non_integer_argument_errors(self):
  1079. a = np.array([[5]])
  1080. assert_raises(TypeError, np.reshape, a, (1., 1., -1))
  1081. assert_raises(TypeError, np.reshape, a, (np.array(1.), -1))
  1082. assert_raises(TypeError, np.take, a, [0], 1.)
  1083. assert_raises(TypeError, np.take, a, [0], np.float64(1.))
  1084. def test_non_integer_sequence_multiplication(self):
  1085. # NumPy scalar sequence multiply should not work with non-integers
  1086. def mult(a, b):
  1087. return a * b
  1088. assert_raises(TypeError, mult, [1], np.float_(3))
  1089. # following should be OK
  1090. mult([1], np.int_(3))
  1091. def test_reduce_axis_float_index(self):
  1092. d = np.zeros((3,3,3))
  1093. assert_raises(TypeError, np.min, d, 0.5)
  1094. assert_raises(TypeError, np.min, d, (0.5, 1))
  1095. assert_raises(TypeError, np.min, d, (1, 2.2))
  1096. assert_raises(TypeError, np.min, d, (.2, 1.2))
  1097. class TestBooleanIndexing:
  1098. # Using a boolean as integer argument/indexing is an error.
  1099. def test_bool_as_int_argument_errors(self):
  1100. a = np.array([[[1]]])
  1101. assert_raises(TypeError, np.reshape, a, (True, -1))
  1102. assert_raises(TypeError, np.reshape, a, (np.bool_(True), -1))
  1103. # Note that operator.index(np.array(True)) does not work, a boolean
  1104. # array is thus also deprecated, but not with the same message:
  1105. assert_raises(TypeError, operator.index, np.array(True))
  1106. assert_warns(DeprecationWarning, operator.index, np.True_)
  1107. assert_raises(TypeError, np.take, args=(a, [0], False))
  1108. def test_boolean_indexing_weirdness(self):
  1109. # Weird boolean indexing things
  1110. a = np.ones((2, 3, 4))
  1111. assert a[False, True, ...].shape == (0, 2, 3, 4)
  1112. assert a[True, [0, 1], True, True, [1], [[2]]].shape == (1, 2)
  1113. assert_raises(IndexError, lambda: a[False, [0, 1], ...])
  1114. def test_boolean_indexing_fast_path(self):
  1115. # These used to either give the wrong error, or incorrectly give no
  1116. # error.
  1117. a = np.ones((3, 3))
  1118. # This used to incorrectly work (and give an array of shape (0,))
  1119. idx1 = np.array([[False]*9])
  1120. assert_raises_regex(IndexError,
  1121. "boolean index did not match indexed array along dimension 0; "
  1122. "dimension is 3 but corresponding boolean dimension is 1",
  1123. lambda: a[idx1])
  1124. # This used to incorrectly give a ValueError: operands could not be broadcast together
  1125. idx2 = np.array([[False]*8 + [True]])
  1126. assert_raises_regex(IndexError,
  1127. "boolean index did not match indexed array along dimension 0; "
  1128. "dimension is 3 but corresponding boolean dimension is 1",
  1129. lambda: a[idx2])
  1130. # This is the same as it used to be. The above two should work like this.
  1131. idx3 = np.array([[False]*10])
  1132. assert_raises_regex(IndexError,
  1133. "boolean index did not match indexed array along dimension 0; "
  1134. "dimension is 3 but corresponding boolean dimension is 1",
  1135. lambda: a[idx3])
  1136. # This used to give ValueError: non-broadcastable operand
  1137. a = np.ones((1, 1, 2))
  1138. idx = np.array([[[True], [False]]])
  1139. assert_raises_regex(IndexError,
  1140. "boolean index did not match indexed array along dimension 1; "
  1141. "dimension is 1 but corresponding boolean dimension is 2",
  1142. lambda: a[idx])
  1143. class TestArrayToIndexDeprecation:
  1144. """Creating an index from array not 0-D is an error.
  1145. """
  1146. def test_array_to_index_error(self):
  1147. # so no exception is expected. The raising is effectively tested above.
  1148. a = np.array([[[1]]])
  1149. assert_raises(TypeError, operator.index, np.array([1]))
  1150. assert_raises(TypeError, np.reshape, a, (a, -1))
  1151. assert_raises(TypeError, np.take, a, [0], a)
  1152. class TestNonIntegerArrayLike:
  1153. """Tests that array_likes only valid if can safely cast to integer.
  1154. For instance, lists give IndexError when they cannot be safely cast to
  1155. an integer.
  1156. """
  1157. def test_basic(self):
  1158. a = np.arange(10)
  1159. assert_raises(IndexError, a.__getitem__, [0.5, 1.5])
  1160. assert_raises(IndexError, a.__getitem__, (['1', '2'],))
  1161. # The following is valid
  1162. a.__getitem__([])
  1163. class TestMultipleEllipsisError:
  1164. """An index can only have a single ellipsis.
  1165. """
  1166. def test_basic(self):
  1167. a = np.arange(10)
  1168. assert_raises(IndexError, lambda: a[..., ...])
  1169. assert_raises(IndexError, a.__getitem__, ((Ellipsis,) * 2,))
  1170. assert_raises(IndexError, a.__getitem__, ((Ellipsis,) * 3,))
  1171. class TestCApiAccess:
  1172. def test_getitem(self):
  1173. subscript = functools.partial(array_indexing, 0)
  1174. # 0-d arrays don't work:
  1175. assert_raises(IndexError, subscript, np.ones(()), 0)
  1176. # Out of bound values:
  1177. assert_raises(IndexError, subscript, np.ones(10), 11)
  1178. assert_raises(IndexError, subscript, np.ones(10), -11)
  1179. assert_raises(IndexError, subscript, np.ones((10, 10)), 11)
  1180. assert_raises(IndexError, subscript, np.ones((10, 10)), -11)
  1181. a = np.arange(10)
  1182. assert_array_equal(a[4], subscript(a, 4))
  1183. a = a.reshape(5, 2)
  1184. assert_array_equal(a[-4], subscript(a, -4))
  1185. def test_setitem(self):
  1186. assign = functools.partial(array_indexing, 1)
  1187. # Deletion is impossible:
  1188. assert_raises(ValueError, assign, np.ones(10), 0)
  1189. # 0-d arrays don't work:
  1190. assert_raises(IndexError, assign, np.ones(()), 0, 0)
  1191. # Out of bound values:
  1192. assert_raises(IndexError, assign, np.ones(10), 11, 0)
  1193. assert_raises(IndexError, assign, np.ones(10), -11, 0)
  1194. assert_raises(IndexError, assign, np.ones((10, 10)), 11, 0)
  1195. assert_raises(IndexError, assign, np.ones((10, 10)), -11, 0)
  1196. a = np.arange(10)
  1197. assign(a, 4, 10)
  1198. assert_(a[4] == 10)
  1199. a = a.reshape(5, 2)
  1200. assign(a, 4, 10)
  1201. assert_array_equal(a[-1], [10, 10])