2017-04-04 10 views
1

を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) 

しかし、割り当て操作は失敗します。 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)の最新バージョンのドキュメントを見ています。

関連する問題