Replace NaN's in NumPy array with closest non-NaN value

前端 未结 7 1712
你的背包
你的背包 2021-02-05 04:22

I have a NumPy array a like the following:

>>> str(a)
\'[        nan         nan         nan  1.44955726  1.44628034  1.44409573\\n  1.4408         


        
相关标签:
7条回答
  • 2021-02-05 05:24

    A recursive solution!

    def replace_leading_NaN(a, offset=0):
        if a[offset].isNaN():
            new_value = replace_leading_NaN(a, offset + 1)
            a[offset] = new_value
            return new_value
        else:
            return a[offset]
    
    def replace_trailing_NaN(a, offset=-1):
        if a[offset].isNaN():
            new_value = replace_trailing_NaN(a, offset - 1)
            a[offset] = new_value
            return new_value
        else:
            return a[offset]
    
    0 讨论(0)
提交回复
热议问题