Skip to content

series.interpolate() corner cases #3674

Closed
@lodagro

Description

@lodagro

from the pydata mailing list

I've been using Series.interpolate(), and have noticed that, while it
leaves NaN entries alone if they precede a non-null value, it blows up if
there are no non-null entries. Wouldn't it make more sense just to leave
completely null series alone (so the user doesn't have to manually check
for this case)? Here's an example:

  1. This one works:
S1 = Series([np.nan, 2.0])
print S1.interpolate()

-------

0   NaN
1     2
dtype: float64
  1. This one blows up:
S2 = Series([np.nan, np.nan])
print S2.interpolate()

ValueError                                Traceback (most recent call last)
<ipython-input-49-620b22122e43> in <module>()
      3
      4 S2 = Series([np.nan, np.nan])
----> 5 print S2.interpolate()

/RHS/packages/anaconda/pandas/pandas/core/series.pyc in interpolate(self,
method)
   3191         result = values.copy()
   3192         result[firstIndex:][invalid] = np.interp(inds[invalid],
inds[valid],
-> 3193
values[firstIndex:][valid])
   3194
   3195         return Series(result, index=self.index, name=self.name)

/Users/stanton/anaconda/lib/python2.7/site-packages/numpy/lib/function_base
.pyc in interp(x, xp, fp, left, right)
   1067         return compiled_interp([x], xp, fp, left, right).item()
   1068     else:
-> 1069         return compiled_interp(x, xp, fp, left, right)
   1070
   1071

ValueError: array of sample points is empty
  1. empty series (not mentioned in the original post)
In [38]: pd.Series([]).interpolate()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-38-012633a78523> in <module>()
----> 1 pd.Series([]).interpolate()

.../pandas/core/series.pyc in interpolate(self, method)
   3184         valid = -invalid
   3185 
-> 3186         firstIndex = valid.argmax()
   3187         valid = valid[firstIndex:]
   3188         invalid = invalid[firstIndex:]

ValueError: attempt to get argmax/argmin of an empty sequence

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions