Skip to content Skip to sidebar Skip to footer

How To Insert Nan Array Into A Numpy 2d Array

I'm trying to insert an arbitrary number of rows of NaN values within a 2D array at specific places. I'm logging some data from a microcontroller in a .csv file and parsing with py

Solution 1:

From the doc of np.insert():

import numpy as np
a = np.arrray([(122.0, 1.0, -47.0), (123.0, 1.0, -47.0), (125.0, 1.0, -44.0)]))
np.insert(a, 2, np.nan, axis=0)
array([[ 122.,    1.,  -47.],
       [ 123.,    1.,  -47.],
       [  nan,   nan,   nan],
       [ 125.,    1.,  -44.]])

Solution 2:

Approach #1

We can use an initialization based approach to handle multiple gaps and gaps of any lengths -

# Pre-processing step to create monotonically increasing array for first col
id_arr = np.zeros(arr.shape[0])
id_arr[np.flatnonzero(np.diff(arr[:,0])<0)+1] = 256
a0 = id_arr.cumsum() + arr[:,0]

range_arr = np.arange(a0[0],a0[-1]+1)
out = np.full((range_arr.shape[0],arr.shape[1]),np.nan)
out[np.in1d(range_arr,a0)] = arr

Sample run -

In [233]: arr      # Input array
Out[233]: 
array([[ 122.,    1.,  -47.],
       [ 123.,    1.,  -47.],
       [ 126.,    1.,  -44.],
       [  39.,    1.,  -47.],
       [  40.,    1.,  -45.],
       [  41.,    1.,  -47.]])

In [234]: out
Out[234]: 
array([[ 122.,    1.,  -47.],
       [ 123.,    1.,  -47.],
       [  nan,   nan,   nan],
       [  nan,   nan,   nan],
       [ 126.,    1.,  -44.],
       [  nan,   nan,   nan], (168 NaN rows)
       .....
       [  nan,   nan,   nan],
       [  nan,   nan,   nan],
       [  39.,    1.,  -47.],
       [  40.,    1.,  -45.],
       [  41.,    1.,  -47.]])

Approach #2

An alternative approach could be suggested to handle such generic cases using np.insert instead of initialization, like so -

idx = np.flatnonzero(~np.in1d(range_arr,a0))
out = np.insert(arr,idx - np.arange(idx.size),np.nan,axis=0)

Post a Comment for "How To Insert Nan Array Into A Numpy 2d Array"