Ric dm
Ric dm

Reputation: 71

How to replace NaNs in array by other array

I have 2 Numpy arrays with the same length

array([ 0.9737068 ,  NaN,  NaN, ..., -0.64236529,
       -0.88137541, -0.78318609])

array([ 0.9 ,  0.7643,  0.61, ..., -0.64236529,
       -0.88137541, -0.78318609])

In the first array I have NaN values, how can I replace these NaN values with values from the second array. In this example my third array would be:

array([ 0.9737068 ,  Nan => 0.7643,  NaN => 0.61 , ..., -0.64236529,
       -0.88137541, -0.78318609])

Upvotes: 3

Views: 2563

Answers (4)

David Buck
David Buck

Reputation: 3829

Using Numpy, the following works by applying a Boolean mask to both arrays:

import numpy as np
x = np.array([0.9737068, np.nan, np.nan, -0.64236529, -0.88137541, -0.78318609])
y = np.array([0.9, 0.7643, 0.61, -0.64236529, -0.88137541, -0.78318609])

x[np.isnan(x)] = y[np.isnan(x)]

Results in

In[1]:  x
Out[1]: 
array([ 0.9737068 ,  0.7643    ,  0.61      , -0.64236529, -0.88137541,
       -0.78318609])

N.B. Running with %timeit, this solution takes < 4µs in repeated runs, vs. the other two Numpy solutions (at time of writing this) which both take 20-25µs

Upvotes: 6

Suraj
Suraj

Reputation: 2477

res = np.array([ a1 if not np.isnan(a1) else b1 for a1,b1 in zip(a, b) ])

np.isnan returns False if the given element in the np.array is np.NaN

Upvotes: 0

StonedTensor
StonedTensor

Reputation: 616

Pythonic solution:

import numpy as np

def no_nans_arrays(array_w_nans,array_no_nans):
    return np.array([array_w_nans[i] if not np.isnan(array_w_nans[i]) else array_no_nans[i] for i in range(len(array_w_nans))])

Upvotes: 1

Daemon Painter
Daemon Painter

Reputation: 3470

If not numpy, you might find this function helpful:

def swap(A,B,i,j):
    TEMP_B = B[j]
    B[j] = A[i]
    A[i] = TEMP_B
    return A,B

Iterate over your first array, if element is NaN, then use swap.

Taken from this question.

Upvotes: 0

Related Questions