Skip to content

Commit

Permalink
Merge branch 'main' into fix_binary_op_literals
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Aug 4, 2023
2 parents 7fee29e + 39d5fcb commit 3a0588a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 14 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## What's changed

- New method `$explode()` for `DataFrame` and `LazyFrame`.
- New method `$explode()` for `DataFrame` and `LazyFrame` (#314).
- New method `$clone()` for `LazyFrame` (#347).
- `$with_column()` is now deprecated (following upstream `polars`). It will be
removed in 0.9.0. It should be replaced with `$with_columns()` (#313).

# polars 0.7.0

Expand Down
1 change: 1 addition & 0 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ DataFrame_with_columns = function(...) {
#' @return DataFrame
#' @details with_column is derived from with_columns but takes only one expression argument
DataFrame_with_column = function(expr) {
warning("`with_column()` is deprecated and will be removed in polars 0.9.0. Please use `with_columns()` instead.")
self$with_columns(expr)
}

Expand Down
7 changes: 6 additions & 1 deletion R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,10 @@ LazyFrame$groupby <- function(exprs, maintain_order) .Call(wrap__LazyFrame__grou

LazyFrame$with_columns <- function(exprs) .Call(wrap__LazyFrame__with_columns, self, exprs)

LazyFrame$with_column <- function(expr) .Call(wrap__LazyFrame__with_column, self, expr)
LazyFrame$with_column <- function(expr) {
warning("`with_column()` is deprecated and will be removed in polars 0.9.0. Please use `with_columns()` instead.")
.Call(wrap__LazyFrame__with_column, self, expr)
}

LazyFrame$with_row_count <- function(name, offset) .Call(wrap__LazyFrame__with_row_count, self, name, offset)

Expand All @@ -955,6 +958,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
2 changes: 1 addition & 1 deletion tests/testthat/test-expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -2282,7 +2282,7 @@ test_that("shrink_dtype", {
f = c("a", "b", "c"),
g = c(0.1, 1.32, 0.12),
h = c(T, NA, F)
)$with_column(pl$col("b")$cast(pl$Int64) * 32L)$select(pl$all()$shrink_dtype())
)$with_columns(pl$col("b")$cast(pl$Int64) * 32L)$select(pl$all()$shrink_dtype())

expect_true(all(mapply(
df$dtypes,
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-expr_arr.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("arr$lengths", {
df = pl$DataFrame(list_of_strs = pl$Series(list(c("a", "b"), "c", character(), list(), NULL)))
l = df$with_column(pl$col("list_of_strs")$arr$lengths()$alias("list_of_strs_lengths"))$to_list()
l = df$with_columns(pl$col("list_of_strs")$arr$lengths()$alias("list_of_strs_lengths"))$to_list()

expect_identical(
l |> lapply(\(x) if (inherits(x, "integer64")) as.numeric(x) else x),
Expand Down Expand Up @@ -424,7 +424,7 @@ test_that("to_struct", {

test_that("eval", {
df = pl$DataFrame(a = list(a = c(1, 8, 3), b = c(4, 5, 2)))
l_act = df$select(pl$all()$cast(pl$dtypes$Float64))$with_column(
l_act = df$select(pl$all()$cast(pl$dtypes$Float64))$with_columns(
pl$concat_list(c("a", "b"))$arr$eval(pl$element()$rank())$alias("rank")
)$to_list()
expect_identical(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-expr_binary.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test_that("bin$encode and bin$decode", {
$cast(pl$Binary)
$alias("base64"))

test_hex_decode = encoded_hex$with_column(
test_hex_decode = encoded_hex$with_columns(
pl$col("hex")$bin$decode("hex")$alias("hex_decoded")
)$select(
c("hex_decoded")
Expand All @@ -65,7 +65,7 @@ test_that("bin$encode and bin$decode", {
)
)$to_list()

test_base64_decode = encoded_base64$with_column(
test_base64_decode = encoded_base64$with_columns(
pl$col("base64")$bin$decode("base64")$alias("base64_decoded")
)$select(
c("base64_decoded")
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))
})
14 changes: 7 additions & 7 deletions tests/testthat/test-lazy_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ test_that("pl$sum", {
# support sum over list of expressions, wildcards or strings
l = list(a = 1:2, b = 3:4, c = 5:6)
expect_identical(
pl$DataFrame(l)$with_column(pl$sum(list("a", "c", 42L)))$to_list(),
pl$DataFrame(l)$with_columns(pl$sum(list("a", "c", 42L)))$to_list(),
c(l, list(sum = c(48L, 50L)))
)
expect_identical(
pl$DataFrame(l)$with_column(pl$sum(list("*")))$to_list(),
pl$DataFrame(l)$with_columns(pl$sum(list("*")))$to_list(),
c(l, list(sum = c(9L, 12L)))
)
expect_identical(
pl$DataFrame(l)$with_column(pl$sum(list(pl$col("a") + pl$col("b"), "c")))$to_list(),
pl$DataFrame(l)$with_columns(pl$sum(list(pl$col("a") + pl$col("b"), "c")))$to_list(),
c(l, list(sum = c(9L, 12L)))
)
})
Expand Down Expand Up @@ -64,13 +64,13 @@ test_that("pl$min pl$max", {

# support sum over list of expressions, wildcards or strings
l = list(a = 1:2, b = 3:4, c = 5:6)
expect_identical(pl$DataFrame(l)$with_column(pl$min(list("a", "c", 42L)))$to_list(), c(l, list(min = c(1:2))))
expect_identical(pl$DataFrame(l)$with_column(pl$max(list("a", "c", 42L)))$to_list(), c(l, list(max = c(42L, 42L))))
expect_identical(pl$DataFrame(l)$with_columns(pl$min(list("a", "c", 42L)))$to_list(), c(l, list(min = c(1:2))))
expect_identical(pl$DataFrame(l)$with_columns(pl$max(list("a", "c", 42L)))$to_list(), c(l, list(max = c(42L, 42L))))


## TODO polars cannot handle wildcards hey wait with testing until after PR
# expect_identical(pl$DataFrame(l)$with_column(pl$max(list("*")))$to_list(),c(l,list(min=c(1:2))))
# expect_identical(pl$DataFrame(l)$with_column(pl$min(list("*")))$to_list(),c(l,list(min=c(1:2))))
# expect_identical(pl$DataFrame(l)$with_columns(pl$max(list("*")))$to_list(),c(l,list(min=c(1:2))))
# expect_identical(pl$DataFrame(l)$with_columns(pl$min(list("*")))$to_list(),c(l,list(min=c(1:2))))
})


Expand Down

0 comments on commit 3a0588a

Please sign in to comment.