0

Another post I had does exactly what I wanted, but I cannot seem to implement on a structured array.

Say I have an array like so:

>>> arr = np.empty(2, dtype=np.dtype([('xy', np.float32, (2, 2))]))
>>> arr['xy']
array([[[1., 1.],
        [2., 2.]],

        [3., 3.],
        [4., 4.]]], dtype=float32)

I need to pad it so that the last row in each subarray is repeated a specific number of times:

arr['xy'] = np.pad(arr['xy'], [(0, 0), (0, 2), (0, 0)], mode='edge')

However I'm getting a ValueError:

ValueError: could not broadcast input array from shape (2, 4, 2) into shape (2, 2, 2)

So without a structured array, I tried the following:

>>> arr = np.array([[[1, 1], [2, 2]], [[3, 3], [4, 4]]])
>>> arr
array([[[1, 1],
        [2, 2]],

        [3, 3],
        [4, 4]]], dtype=float32)
>>> arr = np.pad(arr, [(0, 0), (0, 2), (0, 0)], mode='edge')
>>> arr
array([[[1, 1],
        [2, 2],
        [2, 2],
        [2, 2]],

        [3, 3],
        [4, 4],
        [4, 4],
        [4, 4]], dtype=float32)

How come I cannot repeat with a structured array?

petezurich
  • 9,280
  • 9
  • 43
  • 57
pstatix
  • 3,611
  • 4
  • 18
  • 40

2 Answers2

2

Your padding works, it's the assignment to ar["xy"] that fails, you can't change the shape of a structure.

>>> arr = np.empty(2, dtype=np.dtype([('xy', np.float32, (2, 2))]))
>>> ar2 = np.pad(arr['xy'], [(0, 0), (0, 2), (0, 0)], mode='edge')
>>> ar2.shape
(2, 4, 2)
>>> arr["xy"] = ar2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (2,4,2) into shape (2,2,2)
Cal
  • 631
  • 3
  • 11
0

It might be the late reply, but you can use it instead.

assume that you have structured ndarray input "sample" and pad_value_dict, having {name: pad_value} you want to add.

pad_arr = np.array(
   [tuple([pad_value_dict[k] for k in keys])], 
   dtype=sample.dtype
)
pad_arr = np.tile(pad_arr, pad_len)
result = np.append(sample, pad_arr)