title: Interpolating missing data in Python keeping in mind x values
body: <p>I need a clarification on what tool to use and how to interpolate missing in Python. Refer to the code below:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  from scipy import interpolate
  
  # Create data with missing y values
  x = [i for i in range(0, 10)]
  y = [i**2 + i**3 for i in range(0, 10)]
  y[4] = np.nan
  y[7] = np.nan
  
  # Interpolation attempt 1: Use scipy's interpolate.interp1d
  f = interpolate.interp1d(x, y)
  ynew = f(x)
  
  # Interpolate attempt 2: Use pandas.Series.interpolate
  yp = pd.Series(y)
  yp = yp.interpolate(limit_direction='both', kind='cubic')
  
  plt.plot(x, y, 'o', x, ynew, '-', x, yp, 'x')
  
  plt.show()
  </code></pre>
  
  <p>Code above yields the following diagram</p>
  
  <p><a href="https://i.stack.imgur.com/8EI7G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8EI7G.png" alt="Graph Plot"></a></p>
  
  <p>Notice how the interp1d line (as documented) does not handle NaN values.</p>
  
  <p><strong>My question is: How do I handle NaN values while using the x values as does the scipy's interpolation.interp1d function?</strong></p>
  
  <p>Thanks</p>