I have a numpy structured array where each element in the array is itself a numpy array (dtype='O'). Each element array within the same row always have the same length, while element arrays in different rows can have variable lengths. As an example, it can look something like this:
array([(array([1], dtype=int32), array([0.1], dtype=float64)),
(array([2, 3, 4], dtype=int32), array([0.2, 0.3, 0.4], dtype=float64)),
(array([5, 6], dtype=int32), array([0.5, 0.6], dtype=float64))],
dtype=[('field_1', 'O'), ('field_2', 'O')])
What is the best way to flatten such an array such that rows with element array lengths = N are expanded into N rows? Ideally, I want the flattened array to look like:
array([(1, 0.1),
(2, 0.2),
(3, 0.3),
(4, 0.4),
(5, 0.5),
(6, 0.6)],
dtype=[('field_1', int32), ('field_2', float64)])
But I can also deal with other formats, as long as the rows with length>1 are expanded, e.g.:
array([(array([1], dtype=int32), array([0.1], dtype=float64)),
(array([2], dtype=int32), array([0.2], dtype=float64)),
(array([3], dtype=int32), array([0.3], dtype=float64)),
(array([4], dtype=int32), array([0.4], dtype=float64)),
(array([5], dtype=int32), array([0.5], dtype=float64)),
(array([6], dtype=int32), array([0.6], dtype=float64))],
dtype=[('field_1', 'O'), ('field_2', 'O')])
if that's somehow easier to implement.