Search code examples
dataframerustrust-polars

Removing last row and extending dataframe prints dataframe with the removed row


Why is it when I try and remove the last row a dataframe and insert a new row, it still shows the previous row I tried to remove with the new row?

if self.dataframes.minute()["broker_time"].utf8().unwrap().into_iter().any(|i| i.unwrap() == candle.broker_time()) {
  let size = self.dataframes.get_timeframe(&ETimeFrames::Minute).shape();
   self.dataframes.minute = self.dataframes.minute.head(Some(size.0 - 1));
}
println!("{:#?}", self.dataframes.minute); // prints self.dataframes.minute with the last row removed

let df = df!(..).unwrap() // create df with new data with same columns as original data
self.dataframes.minute.extend(&df).unwrap();
println!("{:#?}", self.dataframes.minute); // shows the row as if it wasn't removed and extends the new df

Solution

  • Fixed by using vstack_mut instead of extend

    vstack_mut works instead of extend because as per the documentation:

    vstack adds the chunks from other to the chunks of this DataFrame

    Whereas extend appends the data from other to the underlying memory locations and thus may cause a reallocation.

    More info can be found in the docs linked above.