Skip to content

Commit

Permalink
Implement .pr$DataFrame$drop_all_in_place() (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorhawell committed Nov 15, 2023
1 parent 1c8e55b commit caa3bd2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
- New methods `$write_json()` and `$write_ndjson()` for DataFrame (#502).
- Removed argument `name` in `pl$date_range()`, which was deprecated for a while
(#503).
- New private method `.pr$DataFrame$drop_all_in_place(df)` to drop `DataFrame` in-place,
to release memory without invoking gc(). However, if there are other strong references to any of
the underlying Series or arrow arrays, that memory will specifically not be released. This method
is aimed for r-polars extensions, and will be kept stable as much as possible (#504).
- New functions `pl$min_horizontal()`, `pl$max_horizontal()`, `pl$sum_horizontal()`,
`pl$all_horizontal()`, `pl$any_horizontal()` (#508).

Expand Down
2 changes: 2 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ DataFrame$default <- function() .Call(wrap__DataFrame__default)

DataFrame$lazy <- function() .Call(wrap__DataFrame__lazy, self)

DataFrame$drop_all_in_place <- function() invisible(.Call(wrap__DataFrame__drop_all_in_place, self))

DataFrame$new_with_capacity <- function(capacity) .Call(wrap__DataFrame__new_with_capacity, capacity)

DataFrame$set_column_from_robj <- function(robj, name) .Call(wrap__DataFrame__set_column_from_robj, self, robj, name)
Expand Down
5 changes: 5 additions & 0 deletions src/rust/src/rdataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ impl DataFrame {
LazyFrame(self.0.clone().lazy())
}

//internal use only
pub fn drop_all_in_place(&mut self) {
*self = Self::new_with_capacity(0);
}

//internal use
pub fn new_with_capacity(capacity: i32) -> Self {
let empty_series: Vec<pl::Series> = Vec::with_capacity(capacity as usize);
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -1206,3 +1206,19 @@ test_that("transpose", {
df_expected
)
})

test_that("drop_all_in_place", {

# this test verifies internal function in_place drop all Series in DataFrame
# will not affect a fully cloned DataFrame df_clone

df_copy = df = polars::pl$DataFrame(mtcars)
df_clone = df$clone()
s = df$get_column("cyl")
.pr$DataFrame$drop_all_in_place(df)
expect_identical(df$shape, c(0,0))
expect_identical(df_copy$shape, c(0,0))
expect_identical(df_clone$shape, c(32,11))
expect_identical(s$len(), 32)
})

0 comments on commit caa3bd2

Please sign in to comment.