-1

I have a large 1D-array with some 'NaN' values dispersed in it. I would like to replace the 'NaN' values with the mean of the values on each side of the 'NaN'.

There is a lot of documentation on this site about replacing 'NaN' with the mean of a column or row, but I want to replace it with just the average of its neighbors.

I have written the following code, but it is not replacing the 'NaN' values at all. Can someone please tell me what I am doing wrong? Thanks in advance.

import numpy as np

lfc=['NaN', 4, 'NaN', 6, 3, 'NaN', 1]

for i in range(0, len(lfc)):
   if lfc[0] == 'nan':
       lfc[0] = lfc[1]
   elif lfc[i] == 'nan':
       lfc[i] = (lfc[i-1] + lfc[i+1]) / 2
   elif lfc[len(lfc)-1] == 'nan':
       lfc[len(lfc)-1] = lfc[len(lfc)-2]

Edit

Sample input:

lfc=['NaN', 4, 'NaN', 6, 3, 'NaN', 1]

Expected output:

lfc=[4, 4, 5, 3, 2, 1]
Community
  • 1
  • 1
72Dayz
  • 1
  • 2
  • Edited in a sample input and expected output. – 72Dayz Feb 19 '20 at 23:51
  • 1
    Do you have actual NumPy arrays, or Python lists? It's difficult to tell because what you shared appears to be somewhere between pseudocode and actual Python, please settle on one and edit your post. – AMC Feb 20 '20 at 00:16
  • 1
    what if the neighbor itself is "nan"!!! – vb_rises Feb 20 '20 at 00:20
  • Post has been edited again- I am dealing with a NumPy array, not a list. As for the nearest neighbor being a 'NaN' as well, I believe this is accounted for by the first part of the loop, as long as the first and second elements aren't NaNs. – 72Dayz Feb 20 '20 at 00:25
  • You are mixing 'nan' and 'NaN' which are not equal. Also, you need to edit your code so that it use `np.ndarray`, not `list`. –  Feb 20 '20 at 00:36
  • Thanks for the help, @vb_rises. I ran the code you posted and it worked fine. When I incorporated it into my code, it did not work. Is there a way to either turn all of my nan's into np.NaN like you did? Or a way to tell what type of NaNs i have? When I pull the nan element from the array and do ```lfc[1]==np.nan```' I get ```false``` – 72Dayz Feb 20 '20 at 00:52
  • @72Dayz then whatever you have, compare with that as the string. for example, you have 'NaN', then compare lfc[1] == 'NaN'. – vb_rises Feb 20 '20 at 00:59
  • @72Dayz I have updated the answer to convert to np.nan. – vb_rises Feb 20 '20 at 01:06
  • Does this answer your question? [Interpolate NaN values in a numpy array](https://stackoverflow.com/questions/6518811/interpolate-nan-values-in-a-numpy-array) –  Feb 20 '20 at 01:06
  • @bombs That worked like a charm! Thank you very much. – 72Dayz Feb 20 '20 at 01:18

1 Answers1

0
# to convert, you can use
lfc=['nan', 4, 'nan', 6, 3, 'nan', 1]
lfc= [np.nan if x == 'nan' else x for x in lfc ]


for i in range(0,len(lfc)):
    if lfc[0] is np.nan:
        lfc[0]=lfc[1]
    elif lfc[i] is np.nan:
        lfc[i]=(lfc[i-1]+lfc[i+1])/2
    elif lfc[len(lfc)-1] is np.nan:
        lfc[len(lfc)-1]=lfc[len(lfc)-2]

 [4, 4, 5.0, 6, 3, 2.0, 1]

NOTE: This does not take care if the neighbor itself is NaN.

vb_rises
  • 1,847
  • 1
  • 9
  • 14