user131983
user131983

Reputation: 3937

Changing a single index of a Series

I am trying to change just a particular index of a Series. The indices are Dates. cashflow_series is the Series, and I am just trying to add a certain number of months to this date by using replace(month = cashflow_series.index[k].month + dict_no_months_between_payments[count]) on the index and use this to replace the previous index date. I am currently using reindex but that would change all the indices and hence I'm stuck as to how to change just one index.

index_here = cashflow_series.index[k]
cashflow_series.index[k] = cashflow_series.index[k].reindex(index=index_here.replace(month = cashflow_series.index[k].month + dict_no_months_between_payments[count]))

Thanks

Upvotes: 2

Views: 744

Answers (1)

Alexander
Alexander

Reputation: 109546

In Pandas, an index is frozen (it is immutable and will give the following error if you try to change a value in the index):

TypeError: '' does not support mutable operations.

You need to create a copy of the old index values, modify it, and then replace the one in your series with this copy. Something like:

idx = df.index.values
idx[3] = '2015-8-15'
df.index = idx

Upvotes: 2

Related Questions