Skip to content
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

Implement .pr$DataFrame$drop_all_in_place() #504

Merged
merged 4 commits into from
Nov 15, 2023
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
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
sorhawell marked this conversation as resolved.
Show resolved Hide resolved
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)
})

Loading