I would have expected True to preserve a ndarray when used as a mask, however, it adds a dimension, just like None.
arr = np.arange(16).reshape(2, 4, 2)
np.all(arr[True] == arr) # outputs: True
Close enough, however looking closer:
arr[True].shape # outputs: (1, 2, 4, 2)
arr[None].shape # outputs: (1, 2, 4, 2)
I found two ways to set an identity mask: using slice(None) or Ellipsis.
np.all(arr[slice(None)] == arr) # outputs: True
arr[slice(None)].shape # outputs: (2, 4, 2)
np.all(Ellipsis == arr) # outputs: True
arr[Ellipsis].shape # outputs: (2, 4, 2)
Nothing really surprising here as this is how slicing works in the first place. slice(None) is a tad ugly and Ellipsis seems a wee bit faster.
However, going through:
I am not sure I fully understand this:
Deprecated since version 1.15.0: In order to remain backward compatible with a common usage in Numeric, basic slicing is also initiated if the selection object is any non-ndarray and non-tuple sequence (such as a list) containing slice objects, the Ellipsis object, or the newaxis object, but not for integer arrays or other embedded sequences.
I understand that the best way to preserve an array is not to mask it, but say I really want to setup a default value for a mask... ;-)
Question: Which is the preferred way to setup an identity mask ? And if I may, is True adding a dimension the intended behavior ?
arr[:]andarr[...]. The return a view. Boolean/mask indexing produces a copy. - hpauljarr.flagsvsq.flagsIfarrwas a "clean" array they should have differentOWNDATAbits. Also, their.baseattributes could be different. But even if their metadata happen to have identical values they are not shared. - Paul PanzerEllipsisis the most general way of indexing an array, and returning the same values. It won't be the same array (in theissense), but will be an identicalview.slice(None)is pretty general as well, but fails with 0d arrays ('scalar'). - hpaulj