无法正确使用 Pandas 对序列进行插值

Unable to correctly use Pandas Interpolate over a series

我正在尝试使用 Pandas、here 提供的插值功能,但由于某种原因,无法让我的系列调整到正确的值。我将它们转换为 float64,但这似乎没有帮助。有什么建议吗?

代码:

for feature in price_data:
    print price_data[feature]
    print "type:"
    print type(price_data[feature])
    newSeries = price_data[feature].astype(float).interpolate()
    print "newSeries: "
    print newSeries

输出:

0    178.9000
1      0.0000
2    178.1200
Name: open_price, dtype: object
type:
<class 'pandas.core.series.Series'>
newSeries: 
0    178.90
1      0.00
2    178.12
Name: open_price, dtype: float64

问题是没有什么可以插值的。我假设您想在零所在的位置插入值。在这种情况下,用 np.nan 替换零然后进行插值。一种方法是

price_data.where(price_data != 0, np.nan).interpolate()

0    178.90
1    178.51
2    178.12
Name: open_price, dtype: float64