I'm looking for an efficient way to repeatedly update rows in a DataFrame. By this I mean changing row values and its index label. I specifically need help with the latter. The best I could find is How to change Pandas dataframe index value? However, this updates the entire index, while I care about a single index label.
Directly assigning to index[n] is not supported:
>>> df.index[1] = 'new_label'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 1374, in __setitem__
raise TypeError("Index does not support mutable operations")
Would it work to modify (assign to) the numpy array underlying the index?
>>> df.index._values[1] = 'new_label'
The updates don't break index sortedness.
More context:
I have a DataFrame indexed by timestamps (DatetimeIndex) where I need to efficiently append new rows in real time (many times per second). I preallocate a large fixed-size DataFrame with NaT/NaN s and I append rows by writing into the next empty row.