title: How to time-efficiently remove values next to 'NaN' values?
body: <p>I'm trying to remove wrong values form my data (a series of 15mln values, 700MB). The values to be removed are values next to 'nan' values, e.g.:</p>
  
  <p>Series: <code>/1/,nan,/2/,3,/4/,nan,nan,nan,/8/,9</code>
  Numbers surrounded by slashes i.e. /1/,/2/,/4/,/8/ are values, which should be removed.</p>
  
  <p>The problem is that it takes way too long to compute that with the following code that I have: </p>
  
  <pre><code>%%time
  
  import numpy as np
  import pandas as pd
  
  # sample data
  speed = np.random.uniform(0,25,15000000)
  next_speed = speed[1:]
  
  # create a dataframe
  data_dict = {'speed': speed[:-1],
              'next_speed': next_speed}
  
  df = pd.DataFrame(data_dict)
  
  
  # calculate difference between the current speed and the next speed
  list_of_differences = []
  
  for i in df.index:
      difference = df.next_speed[i]-df.speed[i]
      list_of_differences.append(difference)
  
  df['difference'] = list_of_differences
  
  # add 'nan' to data in form of a string. 
  
  for i in range(len(df.difference)):
      # arbitrary condition
      if df.difference[i] &lt; -2:
          df.difference[i] = 'nan'
  
  #########################################
  # THE TIME-INEFFICIENT LOOP
  
  # remove wrong values before and after 'nan'.
  for i in  range(len(df)):
  
      # check if the value is a number to skip computations of the following "if" cases
      if not(isinstance(df.difference[i], str)):
          continue
  
      # case 1: where there's only one 'nan' surrounded by values. 
      # Without this case the algo will miss some wrong values because 'nan' will be removed
      # Example of a series: /1/,nan,/2/,3,4,nan,nan,nan,8,9
      # A number surrounded by slashes e.g. /1/ is a value to be removed
      if df.difference[i] == 'nan' and df.difference[i-1] != 'nan' and df.difference[i+1] != 'nan':
          df.difference[i-1]= 'wrong'
          df.difference[i+1]= 'wrong'
  
      # case 2: where the following values are 'nan': /1/, nan, nan, 4
      # E.g.: /1/, nan,/2/,3,/4/,nan,nan,nan,8,9
      elif df.difference[i] == 'nan' and df.difference[i+1] == 'nan':
          df.difference[i-1]= 'wrong'
  
      # case 3: where next value is NOT 'nan'  wrong, nan,nan,4 
          # E.g.: /1/, nan,/2/,3,/4/,nan,nan,nan,/8/,9
      elif df.difference[i] == 'nan' and df.difference[i+1] != 'nan':
          df.difference[i+1]= 'wrong'
  </code></pre>
  
  <p>How to make it more time-efficient?</p>