I am seeing some behavior with Boolean indexing that I do not understand, and I was hoping to find some clarification here.
First off, this is the behavior I am seeking...
>>>
>>> a = np.zeros(10, dtype=np.ndarray)
>>> a
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=object)
>>> b = np.arange(10).reshape(2,5)
>>> b
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> a[5] = b
>>> a
array([0, 0, 0, 0, 0, array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]), 0,
0, 0, 0], dtype=object)
>>>
The reason for choosing an ndarray of ndarrays is because I will be appending the arrays stored in the super array, and they will all be of different lengths. I chose the type ndarray instead of list for the super array so I can have access to all of numpys clever indexing features.
anyway if i make a Boolean indexer and use that to assign, say, b+5 at position 1, it does something I didn't expect
>>> indexer = np.zeros(10,dtype='bool')
>>> indexer
array([False, False, False, False, False, False, False, False, False, False], dtype=bool)
>>> indexer[1] = True
>>> indexer
array([False, True, False, False, False, False, False, False, False, False], dtype=bool)
>>> a[indexer] = b+5
>>> a
array([0, 5, 0, 0, 0, array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]), 0,
0, 0, 0], dtype=object)
>>>
Can anyone help me understand what's going on? I would like the result to be
>>> a[1] = b+5
>>> a
array([0, array([[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]]), 0, 0,
0, array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]), 0, 0, 0, 0], dtype=object)
>>>
The final goal is to have a lot of "b" arrays stored in B, and to assign them to a like this
>>> a[indexer] = B[indexer]
EDIT:
found possible work around based on the discussion below. I can wrap my data in a class if i need to
>>>
>>> class myclass:
... def __init__(self):
... self.data = np.random.rand(1)
...
>>>
>>> b = myclass()
>>> b
<__main__.myclass object at 0x000002871A4AD198>
>>> b.data
array([ 0.40185378])
>>>
>>> a[indexer] = b
>>> a
array([None, <__main__.myclass object at 0x000002871A4AD198>, None, None,
None, None, None, None, None, None], dtype=object)
>>> a[1].data
array([ 0.40185378])
EDIT: this actually fails. I cannot allocate anything to the data field when indexed