I'm trying to replace the last row(s) of a Pandas dataframe using iloc, however I cannot get it to work. There are lots of solutions out there, but the simplest (slowest) is here:
How to do a FIFO push-operation for rows on Pandas dataframe in Python?
Why doesn't this method work in the code below ?
def append_from_dataframe(self,timeframe,new_dataframe):
new_dataframe.reset_index(inplace=True)
temp_dataframe = self.timeframedict.get(timeframe)
num_rows_existing = temp_dataframe.shape[0]
num_rows_new = new_dataframe.shape[0]
overlap = (num_rows_existing + num_rows_new) - 500
# slow, replace with numpy array eventually
if overlap >= 1:
# number of rows to shift
i = overlap * -1
#shift the dataframe back in time
temp_dataframe = temp_dataframe.shift(i)
#self.timeframedict.get(timeframe) = self.timeframedict.get(timeframe).shift(overlap)
#replace the last i rows with the new values
temp_dataframe.iloc[i:] = new_dataframe
self.timeframedict.update({timeframe:temp_dataframe})
else:
#TODO - see this https://stackoverflow.com/questions/10715965/add-one-row-in-a-pandas-dataframe
self.timeframedict.update({timeframe:self.timeframedict.get(timeframe).append(new_dataframe)})
Contents of the dataframe to replace one row in the other:
ipdb> new_dataframe
Timestamp Open High Low Close Volume localtime
0 1533174420000 423.43 423.44 423.43 423.44 0.73765 1533174423776
temp_dataframe.shift(i)
shifts value back one, replaces the values with NaN -
ipdb> temp_dataframe.iloc[i:]
Timestamp Open High Low Close Volume localtime
499 NaN NaN NaN NaN NaN NaN NaN
However temp_dataframe.iloc[i:] = new_dataframe
does not replace anything.
edit: I should add that after some more playing aroundnow I can replace 1 row with:
temp_dataframe.iloc[-1] = new_dataframe.iloc[0]
however, I cannot get the multiple row version to work