2012-12-18 22 views
5

試圖使用非常有用的熊貓來處理數據作爲時間序列,我現在正在陷入這樣一個事實,即似乎沒有可以直接內插(使用樣條或類似方法)數據的DateTime作爲x軸?我似乎總是被迫首先轉換爲浮點數,就像1980年以來的秒數或類似的東西。Python樣條函數或其他在x軸上使用時間的插值?

我嘗試下面的事情,到目前爲止,比較遺憾的是奇怪的格式,我有這樣的東西,只有在IPython的筆記本,我不能從那裏複製單元格:

from scipy.interpolate import InterpolatedUnivariateSpline as IUS 
type(bb2temp): pandas.core.series.TimeSeries 
s = IUS(bb2temp.index.to_pydatetime(), bb2temp, k=1) 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-67-19c6b8883073> in <module>() 
----> 1 s = IUS(bb2temp.index.to_pydatetime(), bb2temp, k=1) 

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/scipy/interpolate/fitpack2.py in __init__(self, x, y, w, bbox, k) 
    335   #_data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier 
    336   self._data = dfitpack.fpcurf0(x,y,k,w=w, 
--> 337          xb=bbox[0],xe=bbox[1],s=0) 
    338   self._reset_class() 
    339 

TypeError: float() argument must be a string or a number 

通過使用bb2temp.index.values(看起來像這些:

array([1970-01-15 184:00:35.884999, 1970-01-15 184:00:58.668999, 
     1970-01-15 184:01:22.989999, 1970-01-15 184:01:45.774000, 
     1970-01-15 184:02:10.095000, 1970-01-15 184:02:32.878999, 
     1970-01-15 184:02:57.200000, 1970-01-15 184:03:19.984000, 

)爲x-說法,有趣的是,樣條類並創造插值,而是試圖插/外推到更大的DateTimeIndex(這是我的最終目標位置)時,它仍然打破。這裏是如何看起來:

all_times = divcal.timed.index.levels[2] # part of a MultiIndex 

all_times 
<class 'pandas.tseries.index.DatetimeIndex'> 
[2009-07-20 00:00:00.045000, ..., 2009-07-20 00:30:00.018000] 
Length: 14063, Freq: None, Timezone: None 

s(all_times.values) # applying the above generated interpolator 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-74-ff11f6d6d7da> in <module>() 
----> 1 s(tall.values) 

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/scipy/interpolate/fitpack2.py in __call__(self, x, nu) 
    219 #   return dfitpack.splev(*(self._eval_args+(x,))) 
    220 #  return dfitpack.splder(nu=nu,*(self._eval_args+(x,))) 
--> 221   return fitpack.splev(x, self._eval_args, der=nu) 
    222 
    223  def get_knots(self): 

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/scipy/interpolate/fitpack.py in splev(x, tck, der, ext) 
    546 
    547   x = myasarray(x) 
--> 548   y, ier =_fitpack._spl_(x, der, t, c, k, ext) 
    549   if ier == 10: 
    550    raise ValueError("Invalid input data") 

TypeError: array cannot be safely cast to required type 

我試圖用s(all_times)s(all_times.to_pydatetime())爲好,用相同的TypeError: array cannot be safely cast to required type

我難過,對嗎?每個人都習慣將時間轉換爲浮點數,所以沒有人認爲這些插值應該自動工作是一個好主意? (我終於找到了一個超級有用的項目來貢獻..)或者你想證明我錯了,並獲得一些SO點? ;)

編輯:警告:檢查你的熊貓數據的NaNs之前,你把它交給插值例程。他們不會抱怨任何事情,只是默默地失敗。

回答

5

問題是那些在下面使用的fitpack例程需要浮動。所以,在某些時候必須有一個從日期時間到浮點數的轉換。這種轉換很容易。如果bb2temp.index.values是你的日期時間排列,只是做:

In [1]: bb2temp.index.values.astype('d') 
Out[1]: 
array([ 1.22403588e+12, 1.22405867e+12, 1.22408299e+12, 
     1.22410577e+12, 1.22413010e+12, 1.22415288e+12, 
     1.22417720e+12, 1.22419998e+12]) 

你只需要傳遞到你的花。要將結果轉換回日期時間對象,您可以執行results.astype('datetime64')

+0

我在想這一定很容易,謝謝!但是,我是否認爲對高級scipy插值例程的補丁可以爲我做到這一點? –

+0

當然,一個補丁會做到這一點。但對我來說似乎仍然過分。它可能是簡單的,如'如果array.dtype =='datetime64':array = array.astype('d')',然後輸出相反。 – tiago

+0

這就是爲什麼我不明白爲什麼他們不這樣做的原因;;)如果他們不爲你工作,爲什麼要在fitpack的頂部引入高水平的課程? splrep和InterpolatedUnivariateSpline之間的許多步驟可能都是簡單的單行或雙行。總之,他們算。 –

相關問題