Most efficient way to forward-fill NaN values in numpy array

后端 未结 5 1026
南笙
南笙 2020-11-28 04:25

Example Problem

As a simple example, consider the numpy array arr as defined below:

import numpy as np
arr = np.array([[5, np.nan, np.         


        
5条回答
  •  清歌不尽
    2020-11-28 05:03

    Here's one approach -

    mask = np.isnan(arr)
    idx = np.where(~mask,np.arange(mask.shape[1]),0)
    np.maximum.accumulate(idx,axis=1, out=idx)
    out = arr[np.arange(idx.shape[0])[:,None], idx]
    

    If you don't want to create another array and just fill the NaNs in arr itself, replace the last step with this -

    arr[mask] = arr[np.nonzero(mask)[0], idx[mask]]
    

    Sample input, output -

    In [179]: arr
    Out[179]: 
    array([[  5.,  nan,  nan,   7.,   2.,   6.,   5.],
           [  3.,  nan,   1.,   8.,  nan,   5.,  nan],
           [  4.,   9.,   6.,  nan,  nan,  nan,   7.]])
    
    In [180]: out
    Out[180]: 
    array([[ 5.,  5.,  5.,  7.,  2.,  6.,  5.],
           [ 3.,  3.,  1.,  8.,  8.,  5.,  5.],
           [ 4.,  9.,  6.,  6.,  6.,  6.,  7.]])
    

提交回复
热议问题