I have created a 2d Numpy string array like so:
a = np.full((2, 3), '#', dtype=np.unicode)
print(a)
The output is:
array([['#', '#', '#'], ['#', '#', '#']], dtype=`'<U1'`)
I would like to pad it with '?' on all sides with a width of 1. I'm expecting output as:
array([
['?', '?', '?', '?', '?'],
['?', '#', '#', '#', '?'],
['?', '#', '#', '#', '?'],
['?', '#', '#', '#', '?'],
['?', '?', '?', '?', '?']],
dtype=`'<U1')
I tried the following:
b = np.pad(a, ((1, 1), (1, 1)), 'constant', constant_values=(('?', '?'), ('?', '?')))
But that gives the following error:
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/numpy/lib/arraypad.py", line 1357, in pad
cast_to_int=False)
File "/usr/lib/python3/dist-packages/numpy/lib/arraypad.py", line 1069, in _normalize_shape
return tuple(tuple(axis) for axis in arr.tolist())
AttributeError: 'tuple' object has no attribute 'tolist'
Similar code works for integers. What am I doing wrong for strings?
numpybug -- it's implementation detail assumes too specific conditions about the array, and it dumps an unhelpful message when it can't go on. As @Kasramvd has shown, you can circumvent it by creating your own padding function.