2017-04-04 22 views
1

我有熊貓作爲對象:大熊貓從TSERIES替換特定元素datetimeindex

df_days 
DatetimeIndex(['2015-05-24', '2015-05-24', '2015-05-24',..., '2016-03-19', 
       '2016-03-19', '2016-03-20'], 
       dtype='datetime64[ns]', length=7224, freq=None) 

type(df_days) 
<class 'pandas.tseries.index.DatetimeIndex'> 

我有索引的陣列爲:

hrIdx 
(array([ 23, 47, 71, 95, 119, 143, 167, 191, 215, 239, 263, 
     287, 311, 335, 359, 383, 407, 431, 455, 479, 503, 527, 
     551, 575, 599, ...,1592], dtype=int64),) 

我試圖執行以下操作:

df_days[hrIdx] = df_days[hrIdx] + td(days=-1) 

但分配操作失敗。減少一天的右側操作很好。

錯誤是:

raise TypeError("Index does not support mutable operations") 
TypeError: Index does not support mutable operations 

是什麼樣我都試過上面來完成改變僅df_days特定元素的正確方法?

編輯:

from datetime import timedelta as td 

回答

2

引用大熊貓文檔,我發現這一點:http://pandas.pydata.org/pandas-docs/stable/timedeltas.html#id1

您可以用代碼一起用這一招:

tdeltas = pd.TimedeltaIndex([str((0-int(i in hrIdx)))+' days' for i in xrange(len(df_days))]) 

然後:

df_days = df_days + tdeltas 

注:我在看最新版本的熊貓文檔(0.19)。