Skip to content

perf: Improve accumulate_dataframes_vertical performance #22399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/polars-core/src/chunked_array/ops/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ where
///
/// See also [`extend`](Self::extend) for appends to the underlying memory
pub fn append(&mut self, other: &Self) -> PolarsResult<()> {
self.append_owned(other.clone())
update_sorted_flag_before_append::<T>(self, other);
let len = self.len();
self.length = self
.length
.checked_add(other.length)
.ok_or_else(|| polars_err!(ComputeError: LENGTH_LIMIT_MSG))?;
self.null_count += other.null_count;
new_chunks(&mut self.chunks, &other.chunks, len);
Ok(())
}

/// Append in place. This is done by adding the chunks of `other` to this [`ChunkedArray`].
Expand Down
28 changes: 28 additions & 0 deletions crates/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,34 @@ impl DataFrame {
Ok(self)
}

pub fn vstack_mut_owned(&mut self, other: DataFrame) -> PolarsResult<&mut Self> {
if self.width() != other.width() {
polars_ensure!(
self.width() == 0,
ShapeMismatch:
"unable to append to a DataFrame of width {} with a DataFrame of width {}",
self.width(), other.width(),
);
self.columns = other.columns;
self.height = other.height;
return Ok(self);
}

self.columns
.iter_mut()
.zip(other.columns.into_iter())
.try_for_each::<_, PolarsResult<_>>(|(left, right)| {
ensure_can_extend(&*left, &right)?;
let right_name = right.name().clone();
left.append_owned(right).map_err(|e| {
e.context(format!("failed to vstack column '{right_name}'").into())
})?;
Ok(())
})?;
self.height += other.height;
Ok(self)
}

/// Concatenate a [`DataFrame`] to this [`DataFrame`]
///
/// If many `vstack` operations are done, it is recommended to call [`DataFrame::align_chunks_par`].
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ where
return Err(width_mismatch(&acc_df, &df));
}

acc_df.vstack_mut(&df)?;
acc_df.vstack_mut_owned(df)?;
}

Ok(acc_df)
Expand Down
Loading