Pedia Pedia - 1 month ago 6
Python Question

How to update the value of DatetimeIndex of a single row in a pandas DataFrame?

In a python pandas DataFrame, I would like to update the value of the index in a single row (preferably in-place as the DataFrame is quite large).

The index is DatetimeIndex and the DataFrame may contain several columns.

For instance:

In [1]: import pandas as pd
In [2]: pd.DataFrame({'DATA': [1,2,3]},
index=[pd.Timestamp(2011,10,01,00,00,00),
pd.Timestamp(2011,10,01,02,00,00),
pd.Timestamp(2011,10,01,03,00,00)])
Out[5]:
DATA
2011-10-01 00:00:00 1
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3


The desired output is:

DATA
2011-10-01 01:00:00 1 <---- Index changed !!!
2011-10-01 02:00:00 2
2011-10-01 03:00:00 3


Is there a simple (and cheap) way to do this for large DataFrames ?

Assuming the location of the sample is known (for instance it is the nth row the needs to be changed) !

Answer

One possible solution with Series.replace, but first need convert Index.to_series:

df.index = df.index
             .to_series()
             .replace({pd.Timestamp('2011-10-01'): pd.Timestamp('2011-10-01 01:00:00')})
print (df)
                     DATA
2011-10-01 01:00:00     1
2011-10-01 02:00:00     2
2011-10-01 03:00:00     3

Another solution with Index.where (new in 0.19.0):

df.index = df.index.where(df.index != pd.Timestamp('2011-10-01'),
                          [pd.Timestamp('2011-10-01 01:00:00')])

print (df)
                     DATA
2011-10-01 01:00:00     1
2011-10-01 02:00:00     2
2011-10-01 03:00:00     3

Solution with appending new row and remove old one by drop, last sort_index:

df.loc[pd.Timestamp('2011-10-01 01:00:00')] = df.loc['2011-10-01 00:00:00', 'DATA']
df.drop(pd.Timestamp('2011-10-01 00:00:00'), inplace=True)
df.sort_index(inplace=True)
print (df)
                     DATA
2011-10-01 01:00:00     1
2011-10-01 02:00:00     2
2011-10-01 03:00:00     3

Another solution if need replace by value not by position:

df.index.set_value(df.index, pd.Timestamp(2011,10,1,0,0,0), pd.Timestamp(2011,10,1,1,0,0))
print (df)
                     DATA
2011-10-01 01:00:00     1
2011-10-01 02:00:00     2
2011-10-01 03:00:00     3

Last solution with converting index to numpy array from comment:

i = 0
df.index.values[i] = pd.Timestamp('2011-10-01 01:00:00')
print (df)          
                     DATA
2011-10-01 01:00:00     1
2011-10-01 02:00:00     2
2011-10-01 03:00:00     3