Skip to content

Commit

Permalink
Add method $clone() for LazyFrame (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Aug 4, 2023
1 parent d91b6a5 commit be1092c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## What's changed

- New method `$explode()` for `DataFrame` and `LazyFrame`.
- New method `$explode()` for `DataFrame` and `LazyFrame` (#314).
- New method `$clone()` for `LazyFrame` (#347).

# polars 0.7.0

Expand Down
2 changes: 2 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ LazyFrame$schema <- function() .Call(wrap__LazyFrame__schema, self)

LazyFrame$explode <- function(columns, dotdotdot_args) .Call(wrap__LazyFrame__explode, self, columns, dotdotdot_args)

LazyFrame$clone_see_me_macro <- function() .Call(wrap__LazyFrame__clone_see_me_macro, self)

#' @export
`$.LazyFrame` <- function (self, name) { func <- LazyFrame[[name]]; environment(func) <- environment(); func }

Expand Down
11 changes: 11 additions & 0 deletions R/lazyframe__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,14 @@ LazyFrame_explode = function(columns = list(), ...) {
.pr$LazyFrame$explode(self, columns, dotdotdot_args) |>
unwrap("in explode():")
}

#' Clone a LazyFrame
#'
#' This makes a very cheap deep copy/clone of an existing `LazyFrame`.
#' @return A LazyFrame
#' @examples
#' pl$LazyFrame(mtcars)$clone()

LazyFrame_clone = function() {
.pr$LazyFrame$clone_see_me_macro(self)
}
17 changes: 17 additions & 0 deletions man/LazyFrame_clone.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/rust/src/lazy/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ impl LazyFrame {
}
Ok(self.0.clone().explode(columns).into())
}

pub fn clone_see_me_macro(&self) -> LazyFrame {
self.clone()
}
}

#[derive(Clone)]
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,12 @@ test_that("with_row_count", {
lf = pl$LazyFrame(mtcars)
expect_identical(lf$with_row_count("idx", 42)$select(pl$col("idx"))$collect()$to_data_frame()$idx, as.double(42:(41+nrow(mtcars))))
})

test_that("cloning", {
pf = pl$LazyFrame(iris)

# deep copy clone rust side object, hence not same mem address
pf2 = pf$clone()
expect_identical(pf$collect()$to_data_frame(), pf2$collect()$to_data_frame())
expect_different(pl$mem_address(pf), pl$mem_address(pf2))
})

0 comments on commit be1092c

Please sign in to comment.