I am trying to delete rows from arrays which are stored inside an object array in numpy. However as you can see it complains that it cannot broadcast the smaller array into the larger array. Works fine when done directly to the array. What is the issue here? Any clean way around this error other than making a new object array and copying one by one until the array I want to modify?
In [1]: import numpy as np
In [2]: x = np.zeros((3, 2))
In [3]: x = np.delete(x, 1, axis=0)
In [4]: x
Out[4]:
array([[ 0., 0.],
[ 0., 0.]])
In [5]: x = np.array([np.zeros((3, 2)), np.zeros((3, 2))], dtype=object)
In [6]: x[0] = np.delete(x[0], 1, axis=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-1687d284d03c> in <module>()
----> 1 x[0] = np.delete(x[0], 1, axis=0)
ValueError: could not broadcast input array from shape (2,2) into shape (3,2)
Edit: Apparently it works when arrays are different shape. This is quite annoying. Any way to disable automatic concatenation by np.array?
In [12]: x = np.array([np.zeros((3, 2)), np.zeros((5, 8))], dtype=object)
In [13]: x[0] = np.delete(x[0], 1, axis=0)
In [14]: x = np.array([np.zeros((3, 2)), np.zeros((3, 2))], dtype=object)
In [15]: x.shape
Out[15]: (2, 3, 2)
In [16]: x = np.array([np.zeros((3, 2)), np.zeros((5, 8))], dtype=object)
In [17]: x.shape
Out[17]: (2,)
This is some quite inconsistent behaviour.
x = np.array([np.zeros((3, 2)), np.zeros((8, 5))], dtype=object)works but not if both are(3, 2). That's annoying.