Javier
Javier

Reputation: 513

How to add nan values in specific index positions in numpy arrays?

I have 2 np.arrays:

The first one called data:

data= array([ 17. ,  nan,   8.1,  25.1,  nan,   6.9,  nan,  27.1,  46.6,
        34.1,  25.7,  nan,  ... , 25.3 ])

Array of float 64 Size (366,)

To get the second one i did an interpolation. So i should first drop the NaN values:

data = data[~numpy.isnan(data)]

So i have now the data like this:

data = array([ 17. ,   8.1,  25.1,   6.9,  27.1,  46.6,
        34.1,  25.7,  ... , 25.3 ])

Array of float 64 Size (283,)

And after the interpolation i get the second one:

interpolated_data = array([ 16 ,   7.1,  24.1,   7.9,  26.1,  45.6,
            33.1,  27.7,  ... , 24.3 ])

Array of float 64 Size (283,)

Now i want to give it back the nan values in the same index position in both arrays.

Expected values:

data = array([ 17. ,  nan,   8.1,  25.1,  nan,   6.9,  nan,  27.1,  46.6,
            34.1,  25.7,  nan,  ... , 25.3 ])

Array of float 64 Size (366,)

interpolated_data = array([ 16 ,  nan,   7.1,  24.1,  nan,   7.9,  nan,  26.1,  45.6,
            33.1,  27.7,  nan,  ... , 24.3 ])

Array of float 64 Size (366,)

Would you mind to help me? Thanks in advance.

Upvotes: 1

Views: 1423

Answers (3)

Romain Simon
Romain Simon

Reputation: 355

First your extract the values from your data array with the mask you created:

data= array([ 17. ,  nan,   8.1,  25.1,  nan,   6.9,  nan,  27.1,  46.6,
    34.1,  25.7,  nan,  ... , 25.3 ])

nan_mask = numpy.isnan(data)
data1 = data[~nan_mask]

From there you get your interpolated_data. Then, you can create an empty array of the same size of the initial data array and then put back your interpolated_data and the np.nan in this empty array:

interpolated_array = np.empty(data.shape)
interpolated_array[~nan_mask] = interpolated_data
interpolated_array[nan_mask] = np.nan

Upvotes: 2

sehan2
sehan2

Reputation: 1835

Here you go:

# generate data with nan values
data = np.ones(10)
data[4] = np.nan

# get boolean selection where data is nan
boolean_selection = np.isnan(data)

# apply some interpolation on the data that is not nan
# this is just a placeholder
interpolated_data = data[np.logical_not(boolean_selection)]

# fill back the interpolated data
data[np.logical_not(boolean_selection)] = interpolated_data

Upvotes: 0

Corralien
Corralien

Reputation: 120409

Keep index where there is no nan, do computation and recreate an array with the same dimension as the initial filled by nan. Use your index to copy your value to the new array.

# initial array
a = array([ 1.,  2., nan,  4., nan,  6.])

# index where no nan
idx = np.where(~np.isnan(a))

# new array without nan
m = a[idx]
print(m)
array([1., 2., 4., 6.])

# ... interpolation ...
print(i)
array([10, 20, 40, 60])

# replace nan
b = np.array([np.nan] * len(a))
new[idx] = i
print(b)
array([10., 20., nan, 40., nan, 60.])

Upvotes: 0

Related Questions