Skip to content

Commit

Permalink
remove old (now wrong behavior), add optional warning
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Nov 14, 2023
1 parent 2474699 commit 20fd2c5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 81 deletions.
8 changes: 4 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
- New methods `$write_json()` and `$write_ndjson()` for DataFrame (#502).
- Removed argument `name` in `pl$date_range()`, which was deprecated for a while
(#503).
- The rowwise computation when several columns are passed to `pl$min()` and
`pl$max()` is deprecated and will be removed in 0.12.0. Use `pl$min_horizontal()`
`pl$max_horizontal()` instead.
- The rowwise computation when several columns are passed to `pl$min()`, `pl$max()`
and `pl$sum()` is deprecated and will be removed in 0.12.0. Use `pl$min_horizontal()`
`pl$max_horizontal()`, and `pl$sum_horizontal()` instead (#508).
- New functions `pl$min_horizontal()`, `pl$max_horizontal()`,`pl$sum_horizontal()`,
`pl$all_horizontal()`, `pl$any_horizontal()`.
`pl$all_horizontal()`, `pl$any_horizontal()` (#508).

# polars 0.10.1

Expand Down
84 changes: 46 additions & 38 deletions R/functions__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,11 @@ pl$approx_n_unique = function(column) { #-> int or Expr
}


#' sum across expressions / literals / Series
#' @description syntactic sugar for starting a expression with sum
#' Compute sum in one or several columns
#'
#' This is syntactic sugar for `pl$col(...)$sum()`.
#' @name pl_sum
#'
#' @param ... is a:
#' If one arg:
#' - Series or Expr, same as `column$sum()`
Expand All @@ -464,6 +466,9 @@ pl$approx_n_unique = function(column) { #-> int or Expr
#' - list of strings(column names) or expressions to add up as expr1 + expr2 + expr3 + ...
#'
#' If several args, then wrapped in a list and handled as above.
#' @param verbose Show the deprecation message when several columns or Expr are
#' passed in `...`. Will be removed in 0.12.0.
#'
#' @return Expr
#' @keywords Expr_new
#' @examples
Expand All @@ -473,20 +478,12 @@ pl$approx_n_unique = function(column) { #-> int or Expr
#' # column as Expr (prefer pl$col("Petal.Width")$sum())
#' pl$DataFrame(iris)$select(pl$sum(pl$col("Petal.Width")))
#'
#' # column as numeric
#' pl$DataFrame()$select(pl$sum(1:5))
#'
#'
#' df = pl$DataFrame(a = 1:2, b = 3:4, c = 5:6)
#'
#' # column as list
#' df$with_columns(pl$sum(list("a", "c")))
#' df$with_columns(pl$sum(list("a", "c", 42L)))
#'
#' # two eqivalent lines
#' df$with_columns(pl$sum(list(pl$col("a") + pl$col("b"), "c")))
#' df$with_columns(pl$sum(list("*")))
pl$sum = function(...) {
#' # Compute sum in several columns
#' df$with_columns(pl$sum("a", "c"))
#' df$with_columns(pl$sum("*"))
pl$sum = function(..., verbose = TRUE) {
column = list2(...)
if (length(column) == 1L) column <- column[[1L]]
if (inherits(column, "Series") || inherits(column, "Expr")) {
Expand All @@ -499,38 +496,43 @@ pl$sum = function(...) {
return(pl$lit(column)$sum())
}
if (is.list(column)) {
pra = do.call(construct_ProtoExprArray, column)
return(sum_exprs(pra))
if (verbose) {
warning("This usage of `pl$sum()` used to compute the sum rowwise. This is now deprecated, use `pl$sum_horizontal()` instead. This message will be removed in 0.12.0. Set `verbose = FALSE` to remove this message.")
}
return(pl$col(column)$sum())
}
stop("pl$sum: this input is not supported")
}


#' min across expressions / literals / Series
#' @description Folds the expressions from left to right, keeping the first non-null value.
#' Find minimum value in one or several columns
#'
#' This is syntactic sugar for `pl$col(...)$min()`.
#' @name pl_min
#' @param ... is a:
#' If one arg:
#' - Series or Expr, same as `column$sum()`
#' - string, same as `pl$col(column)$sum()`
#' - numeric, same as `pl$lit(column)$sum()`
#' - list of strings(column names) or expressions to add up as expr1 + expr2 + expr3 + ...
#'
#' If several args, then wrapped in a list and handled as above.
#' @param verbose Show the deprecation message when several columns or Expr are
#' passed in `...`. Will be removed in 0.12.0.
#'
#' @return Expr
#' @keywords Expr_new
#' @examples
#' df = pl$DataFrame(
#' a = NA_real_,
#' b = c(2:1, NA_real_, NA_real_),
#' c = c(1:3, NA_real_),
#' d = c(1:2, NA_real_, -Inf)
#' b = c(1:2, NA_real_, NA_real_),
#' c = c(1:4)
#' )
#' # use min to get first non Null value for each row, otherwise insert 99.9
#' df
#'
#' df$with_columns(
#' pl$min("a", "b", "c", 99.9)$alias("d")
#' pl$min("a", "b", "c")
#' )
pl$min = function(...) {
pl$min = function(..., verbose = TRUE) {
column = list2(...)
if (length(column) == 1L) column <- column[[1L]]
if (inherits(column, "Series") || inherits(column, "Expr")) {
Expand All @@ -543,9 +545,10 @@ pl$min = function(...) {
return(pl$lit(column)$min())
}
if (is.list(column)) {
warning("This usage of `pl$min()` is deprecated. To find the minimum value rowwise, use `pl$min_horizontal()` instead. This will be removed in 0.12.0.")
pra = do.call(construct_ProtoExprArray, column)
return(min_horizontal(pra))
if (verbose) {
warning("This usage of `pl$min()` used to find the minimum value rowwise. This is now deprecated, use `pl$min_horizontal()` instead. This message will be removed in 0.12.0. Set `verbose = FALSE` to remove this message.")
}
return(pl$col(column)$min())
}
stop("pl$min: this input is not supported")
}
Expand All @@ -554,8 +557,9 @@ pl$min = function(...) {



#' max across expressions / literals / Series
#' @description Folds the expressions from left to right, keeping the first non-null value.
#' Find maximum value in one or several columns
#'
#' This is syntactic sugar for `pl$col(...)$max()`.
#' @name pl_max
#' @param ... is a:
#' If one arg:
Expand All @@ -565,20 +569,23 @@ pl$min = function(...) {
#' - list of strings(column names) or expressions to add up as expr1 + expr2 + expr3 + ...
#'
#' If several args, then wrapped in a list and handled as above.
#' @param verbose Show the deprecation message when several columns or Expr are
#' passed in `...`. Will be removed in 0.12.0.
#'
#' @return Expr
#' @keywords Expr_new
#' @examples
#' df = pl$DataFrame(
#' a = NA_real_,
#' b = c(1:2, NA_real_, NA_real_),
#' c = c(1:3, NA_real_)
#' c = c(1:4)
#' )
#' # use coalesce to get first non Null value for each row, otherwise insert 99.9
#' df
#'
#' df$with_columns(
#' pl$coalesce("a", "b", "c", 99.9)$alias("d")
#' pl$max("a", "b", "c")
#' )
#'
pl$max = function(...) {
pl$max = function(..., verbose = TRUE) {
column = list2(...)
if (length(column) == 1L) column <- column[[1L]]
if (inherits(column, "Series") || inherits(column, "Expr")) {
Expand All @@ -591,9 +598,10 @@ pl$max = function(...) {
return(pl$lit(column)$max())
}
if (is.list(column)) {
warning("This usage of `pl$max()` is deprecated. To find the maximum value rowwise, use `pl$max_horizontal()` instead. This will be removed in 0.12.0.")
pra = do.call(construct_ProtoExprArray, column)
return(max_exprs(pra))
if (verbose) {
warning("This usage of `pl$max()` used to find the maximum value rowwise. This is now deprecated, use `pl$max_horizontal()` instead. This message will be removed in 0.12.0. Set `verbose = FALSE` to remove this message.")
}
return(pl$col(column)$max())
}
stop("pl$max: this input is not supported")
}
Expand Down
15 changes: 9 additions & 6 deletions man/pl_max.Rd

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

20 changes: 11 additions & 9 deletions man/pl_min.Rd

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

21 changes: 8 additions & 13 deletions man/pl_sum.Rd

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

28 changes: 17 additions & 11 deletions tests/testthat/test-lazy_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ test_that("pl$sum", {
# for now
l = list(a = 1:2, b = 3:4, c = 5:6)
expect_identical(
pl$DataFrame(l)$with_columns(pl$sum(list("a", "c", 42L))$shrink_dtype())$to_list(),
c(l, list(sum = c(48L, 50L)))
pl$DataFrame(l)$with_columns(pl$sum("a", "c", verbose = FALSE)$shrink_dtype())$to_list(),
list(a = c(3L, 3L), b = c(3L, 4L), c = c(11L, 11L))
)
expect_identical(
pl$DataFrame(l)$with_columns(pl$sum(list("*"))$shrink_dtype())$to_list(),
c(l, list(sum = c(9L, 12L)))
)
expect_identical(
pl$DataFrame(l)$with_columns(pl$sum(list(pl$col("a") + pl$col("b"), "c"))$shrink_dtype())$to_list(),
c(l, list(sum = c(9L, 12L)))
pl$DataFrame(l)$with_columns(pl$sum("*", verbose = FALSE)$shrink_dtype())$to_list(),
list(a = c(3L, 3L), b = c(7L, 7L), c = c(11L, 11L))
)
})

Expand Down Expand Up @@ -64,10 +60,20 @@ test_that("pl$min pl$max", {
expect_identical(df$to_list()[[1L]], 1L)


# support sum over list of expressions, wildcards or strings
# support operation over list of expressions, wildcards or strings
l = list(a = 1:2, b = 3:4, c = 5:6)
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))))
expect_identical(
pl$DataFrame(l)$
with_columns(pl$min("a", "c", verbose = FALSE))$
to_list(),
list(a = c(1L, 1L), b = c(3L, 4L), c = c(5L, 5L))
)
expect_identical(
pl$DataFrame(l)$
with_columns(pl$max("a", "c", verbose = FALSE))$
to_list(),
list(a = c(2L, 2L), b = c(3L, 4L), c = c(6L, 6L))
)


## TODO polars cannot handle wildcards hey wait with testing until after PR
Expand Down

0 comments on commit 20fd2c5

Please sign in to comment.