Skip to content

Commit e1fd75c

Browse files
authored
perf: Improve accumulate_dataframes_vertical performance (#22399)
1 parent cca795d commit e1fd75c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

crates/polars-core/src/chunked_array/ops/append.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,15 @@ where
149149
///
150150
/// See also [`extend`](Self::extend) for appends to the underlying memory
151151
pub fn append(&mut self, other: &Self) -> PolarsResult<()> {
152-
self.append_owned(other.clone())
152+
update_sorted_flag_before_append::<T>(self, other);
153+
let len = self.len();
154+
self.length = self
155+
.length
156+
.checked_add(other.length)
157+
.ok_or_else(|| polars_err!(ComputeError: LENGTH_LIMIT_MSG))?;
158+
self.null_count += other.null_count;
159+
new_chunks(&mut self.chunks, &other.chunks, len);
160+
Ok(())
153161
}
154162

155163
/// Append in place. This is done by adding the chunks of `other` to this [`ChunkedArray`].

crates/polars-core/src/frame/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,34 @@ impl DataFrame {
11681168
Ok(self)
11691169
}
11701170

1171+
pub fn vstack_mut_owned(&mut self, other: DataFrame) -> PolarsResult<&mut Self> {
1172+
if self.width() != other.width() {
1173+
polars_ensure!(
1174+
self.width() == 0,
1175+
ShapeMismatch:
1176+
"unable to append to a DataFrame of width {} with a DataFrame of width {}",
1177+
self.width(), other.width(),
1178+
);
1179+
self.columns = other.columns;
1180+
self.height = other.height;
1181+
return Ok(self);
1182+
}
1183+
1184+
self.columns
1185+
.iter_mut()
1186+
.zip(other.columns.into_iter())
1187+
.try_for_each::<_, PolarsResult<_>>(|(left, right)| {
1188+
ensure_can_extend(&*left, &right)?;
1189+
let right_name = right.name().clone();
1190+
left.append_owned(right).map_err(|e| {
1191+
e.context(format!("failed to vstack column '{right_name}'").into())
1192+
})?;
1193+
Ok(())
1194+
})?;
1195+
self.height += other.height;
1196+
Ok(self)
1197+
}
1198+
11711199
/// Concatenate a [`DataFrame`] to this [`DataFrame`]
11721200
///
11731201
/// If many `vstack` operations are done, it is recommended to call [`DataFrame::align_chunks_par`].

crates/polars-core/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ where
815815
return Err(width_mismatch(&acc_df, &df));
816816
}
817817

818-
acc_df.vstack_mut(&df)?;
818+
acc_df.vstack_mut_owned(df)?;
819819
}
820820

821821
Ok(acc_df)

0 commit comments

Comments
 (0)