Python插值问题
我有csv格式的时间序列流量数据:Python插值问题,python,pandas,time-series,interpolation,Python,Pandas,Time Series,Interpolation,我有csv格式的时间序列流量数据: ... 2015-01-04 08:29:05,271238 2015-01-04 08:34:05,329285 2015-01-04 08:39:05,-1 2015-01-04 08:44:05,260260 2015-01-04 08:49:05,263711 ... 时间序列中有一些缺失的数据(也是连续的),加上它的间隔不是相等的。我想做的是,使用Pandas尽可能地插值缺失的值,然后以5分钟的间隔重新索引时间序列 我将interpolate()方
...
2015-01-04 08:29:05,271238
2015-01-04 08:34:05,329285
2015-01-04 08:39:05,-1
2015-01-04 08:44:05,260260
2015-01-04 08:49:05,263711
...
时间序列中有一些缺失的数据(也是连续的),加上它的间隔不是相等的。我想做的是,使用Pandas尽可能地插值缺失的值,然后以5分钟的间隔重新索引时间序列
我将interpolate()
方法与quadratic
、slinear
和cubic
参数一起使用,但我对结果不太满意。我想尝试分段多项式
方法,但我得到以下错误:
ZeroDivisionError: integer division or modulo by zero
我检查了我的数据,没有空/零条目。有人声称这是Python版本的问题(我使用的是Python 2.6),并建议添加以下导入:
from __future__ import division
但它没有起作用。这是实际代码:
values = [np.nan if x == -1 else x for x in self.y]
convertedIndex = [mktime(dateIndex.timetuple()) for dateIndex in self.x]
ts = pd.TimeSeries(values[:5000], index=self.x[:5000])
self.interpolatedValues = ts.interpolate(method='spline', order=2, downcast='infer').tolist()
在self.x中有日期,而在self.y中有时间序列值。我已将缺少的值(-1)替换为np.nan。我已经尝试对日期使用datetime和mktime格式
如有任何建议,我们将不胜感激。谢谢大家
下面是一些再现ZeroDivision错误的代码:
import numpy as np
import pandas as pd
np.random.seed(2015)
def random_dates(start, end, size):
dates = [np.datetime64(date).astype('<M8[ns]').view('<i8')
for date in [start, end]]
return np.random.randint(*dates, size=size).view('<M8[ns]')
N = 5
dates = np.sort(random_dates('2015-05-27 00:00:00', '2015-05-27 02:00:00', N))
ts = pd.Series(np.random.randint(100, size=N), index=dates)
ts = ts.resample('5T')
ts = ts.interpolate(method='piecewise_polynomial')
将numpy导入为np
作为pd进口熊猫
np.random.seed(2015年)
def随机_日期(开始、结束、大小):
dates=[np.datetime64(date).astype('欢迎使用SO,请发布导致错误的实际代码,以及您的numpy和pandas版本,谢谢,请发布代码。另外,“对结果不太满意”到底是什么意思?明确地写下你想要达到的确切结果通常是很有帮助的。@riccamini:我添加了一些代码来复制ZeroDivision错误。请检查这些代码是否准确地模拟了你的情况。我用这些代码编辑了答案。我使用的是numpy 1.8.2和pandas 0.16.0。@JohnE我不满意,因为通常,插值是巨大的负数,它们作为旅行时间数据毫无意义。可能您陷入了错误