heresthebuzz
heresthebuzz

Reputation: 719

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)

Upvotes: 7

Views: 7016

Answers (3)

hpaulj
hpaulj

Reputation: 231510

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]])

Upvotes: 4

sammywemmy
sammywemmy

Reputation: 28709

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]])

Upvotes: 2

cs95
cs95

Reputation: 402813

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.

Upvotes: 12

Related Questions