'np.reshape() with padding if there are not enough elements

Is it possible to reshape a np.array() and, in case of inconsistency of the new shape, fill the empty spaces with NaN?

Ex:

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

Target, for instance a 2x4 Matrix:

[1 2  3   4]
[5 6 NaN NaN]

I need this to bypass the error: ValueError: cannot reshape array of size 6 into shape (2,4)



Solution 1:[1]

We'll use np.pad first, then reshape:

m, n = 2, 4
np.pad(arr.astype(float), (0, m*n - arr.size), 
       mode='constant', constant_values=np.nan).reshape(m,n)


array([[ 1.,  2.,  3.,  4.],
       [ 5.,  6., nan, nan]])

The assumption here is that arr is a 1D array. Add an assertion before this code to fail on unexpected cases.

Solution 2:[2]

Lots of ways of doing this, but (nearly) all amount to creating a new array of the desired shape, and filling values:

In [50]: arr = np.array([1,2,3,4,5,6])                                          
In [51]: res = np.full((2,4), np.nan)                                           
In [52]: res                                                                    
Out[52]: 
array([[nan, nan, nan, nan],
       [nan, nan, nan, nan]])
In [53]: res.flat[:len(arr)]=arr                                                
In [54]: res                                                                    
Out[54]: 
array([[ 1.,  2.,  3.,  4.],
       [ 5.,  6., nan, nan]])

I used flat to treat res as a 1d array for copy purposes.

An exception is the resize method, but that fills with 0s. And doesn't change the dtype to allow for float nan:

In [55]: arr.resize(2,4)                                                        
In [56]: arr                                                                    
Out[56]: 
array([[1, 2, 3, 4],
       [5, 6, 0, 0]])

Solution 3:[3]

One possible solution :

convert array to float (nan is a float type)

arr = np.array([1,2,3,4,5,6]).astype(float)

resize data to new shape

arr = np.resize(arr, (2,4))

print(arr)

array([[1., 2., 3., 4.],
   [5., 6., 1., 2.]])

replace last two entries with np.NaN

arr[-1,-2:] = np.NaN

print(arr)

array([[ 1.,  2.,  3.,  4.],
       [ 5.,  6., nan, nan]])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cs95
Solution 2 hpaulj
Solution 3 sammywemmy