Reputation: 4807
I have pandas object as:
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'>
I have an array of indexes as:
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),)
I am trying the following:
df_days[hrIdx] = df_days[hrIdx] + td(days=-1)
But the assignment operation fails. The right side operation of substracting a day works fine.
Error is:
raise TypeError("Index does not support mutable operations")
TypeError: Index does not support mutable operations
What is proper way of changing only specific elements of df_days
like I have tried to accomplish above?
Edit:
from datetime import timedelta as td
Upvotes: 1
Views: 250
Reputation: 1240
Referencing the pandas docs, I found this: http://pandas.pydata.org/pandas-docs/stable/timedeltas.html#id1
You can use this trick along with that code:
tdeltas = pd.TimedeltaIndex([str((0-int(i in hrIdx)))+' days' for i in xrange(len(df_days))])
and then:
df_days = df_days + tdeltas
Note: I'm looking at the docs from the latest version of pandas (0.19).
Upvotes: 2