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

Chore: update conversions and error-handling #516

Merged
merged 14 commits into from
Dec 1, 2023
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
columns to these functions will now compute the min/max/sum in each column
separately. Use `pl$min_horizontal()` `pl$max_horizontal()`, and
`pl$sum_horizontal()` instead for rowwise computation (#508).
- `pl$concat_list()`: elements being strings are now interpreted as column names. Use `pl$lit` to
concat with a string.
sorhawell marked this conversation as resolved.
Show resolved Hide resolved
- `$is_not()` is deprecated and will be removed in 0.12.0. Use `$not()` instead
(#511, #531).
- `$is_first()` is deprecated and will be removed in 0.12.0. Use `$is_first_distinct()`
Expand Down
27 changes: 18 additions & 9 deletions R/expr__expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,8 @@ Expr_fill_nan = function(expr = NULL) {
#' @examples
#' pl$select(pl$lit(1:5)$std())
Expr_std = function(ddof = 1) {
unwrap(.pr$Expr$std(self, ddof))
.pr$Expr$std(self, ddof) |>
unwrap("in $std():")
}

#' Get variance
Expand All @@ -1722,7 +1723,8 @@ Expr_std = function(ddof = 1) {
#' @examples
#' pl$select(pl$lit(1:5)$var())
Expr_var = function(ddof = 1) {
unwrap(.pr$Expr$var(self, ddof))
.pr$Expr$var(self, ddof) |>
unwrap("in $var():")
}

#' Get maximum value
Expand Down Expand Up @@ -2657,6 +2659,7 @@ Expr_rank = function(method = "average", descending = FALSE) {
unwrap(.pr$Expr$rank(self, method, descending))
}


#' Difference
#'
#' Calculate the n-th discrete difference.
Expand All @@ -2669,8 +2672,9 @@ Expr_rank = function(method = "average", descending = FALSE) {
#' diff_default = pl$col("a")$diff(),
#' diff_2_ignore = pl$col("a")$diff(2, "ignore")
#' )
Expr_diff = function(n = 1, null_behavior = "ignore") {
unwrap(.pr$Expr$diff(self, n, null_behavior))
Expr_diff = function(n = 1, null_behavior = c("ignore", "drop")) {
.pr$Expr$diff(self, n, null_behavior) |>
unwrap("in $diff():")
}

#' Percentage change
Expand Down Expand Up @@ -3072,7 +3076,8 @@ Expr_ewm_mean = function(
com = NULL, span = NULL, half_life = NULL, alpha = NULL,
adjust = TRUE, min_periods = 1L, ignore_nulls = TRUE) {
alpha = prepare_alpha(com, span, half_life, alpha)
unwrap(.pr$Expr$ewm_mean(self, alpha, adjust, min_periods, ignore_nulls))
.pr$Expr$ewm_mean(self, alpha, adjust, min_periods, ignore_nulls) |>
unwrap("in $ewm_mean()")
}

#' Exponentially-weighted moving standard deviation
Expand All @@ -3087,7 +3092,8 @@ Expr_ewm_std = function(
com = NULL, span = NULL, half_life = NULL, alpha = NULL,
adjust = TRUE, bias = FALSE, min_periods = 1L, ignore_nulls = TRUE) {
alpha = prepare_alpha(com, span, half_life, alpha)
unwrap(.pr$Expr$ewm_std(self, alpha, adjust, bias, min_periods, ignore_nulls))
.pr$Expr$ewm_std(self, alpha, adjust, bias, min_periods, ignore_nulls) |>
unwrap("in $ewm_std()")
}

#' Exponentially-weighted moving variance
Expand All @@ -3102,7 +3108,8 @@ Expr_ewm_var = function(
com = NULL, span = NULL, half_life = NULL, alpha = NULL,
adjust = TRUE, bias = FALSE, min_periods = 1L, ignore_nulls = TRUE) {
alpha = prepare_alpha(com, span, half_life, alpha)
unwrap(.pr$Expr$ewm_var(self, alpha, adjust, bias, min_periods, ignore_nulls))
.pr$Expr$ewm_var(self, alpha, adjust, bias, min_periods, ignore_nulls) |>
unwrap("in $ewm_var()")
}

#' Extend Series with a constant
Expand All @@ -3116,7 +3123,8 @@ Expr_ewm_var = function(
#' pl$select(pl$lit(1:4)$extend_constant(10.1, 2))
#' pl$select(pl$lit(1:4)$extend_constant(NULL, 2))
Expr_extend_constant = function(value, n) {
unwrap(.pr$Expr$extend_constant(self, wrap_e(value), n))
.pr$Expr$extend_constant(self, wrap_e(value), n) |>
unwrap("in $extend_constant()")
}

#' Repeat a Series
Expand All @@ -3134,7 +3142,8 @@ Expr_extend_constant = function(value, n) {
#' pl$select(pl$lit("alice")$rep(n = 3))
#' pl$select(pl$lit(1:3)$rep(n = 2))
Expr_rep = function(n, rechunk = TRUE) {
unwrap(.pr$Expr$rep(self, n, rechunk))
.pr$Expr$rep(self, n, rechunk) |>
unwrap("in $rep()")
}

#' Extend a Series by repeating values
Expand Down
22 changes: 14 additions & 8 deletions R/expr__list.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ ExprList_unique = function() .pr$Expr$list_unique(self)
#' )
#' df$select(pl$col("a")$list$concat(pl$col("b")))
#'
#' df$select(pl$col("a")$list$concat("hello from R"))
#' df$select(pl$col("a")$list$concat(pl$lit("hello from R")))
#'
#' df$select(pl$col("a")$list$concat(list("hello", c("hello", "world"))))
#' df$select(pl$col("a")$list$concat(pl$lit(list("hello", c("hello", "world")))))
ExprList_concat = function(other) {
pl$concat_list(list(self, other))
}
Expand Down Expand Up @@ -283,9 +283,10 @@ ExprList_arg_max = function() .pr$Expr$list_arg_max(self)
#' @aliases Expr_list_diff
#' @examples
#' df = pl$DataFrame(list(s = list(1:4, c(10L, 2L, 1L))))
#' df$select(pl$col("s")$list$diff())
ExprList_diff = function(n = 1, null_behavior = "ignore") {
unwrap(.pr$Expr$list_diff(self, n, null_behavior))
#' df$select(pl$col("s")$list$diff(1))
ExprList_diff = function(n = 1, null_behavior = c("ignore", "drop")) {
.pr$Expr$list_diff(self, n, null_behavior) |>
unwrap("in $list$diff()")
}

#' Shift sublists
Expand Down Expand Up @@ -364,7 +365,9 @@ ExprList_tail = function(n = 5L) {

#' List to Struct
#' @param n_field_strategy Strategy to determine the number of fields of the struct.
#' default = 'first_non_null' else 'max_width'
#' default = "first_non_null": set number of fields equal to the length of the
#' first non zero-length sublist. else 'max_width'; else "max_width":
#' set number of fields as max length of all sublists.
#' @param name_generator an R function that takes an R scalar double and outputs
#' a string value. It is a f64 because i32 might not be a big enough enumerate all.
#' The default (`NULL`) is equivalent to the R function
Expand All @@ -390,7 +393,10 @@ ExprList_tail = function(n = 5L) {
#'
#' df2$to_list()
ExprList_to_struct = function(
n_field_strategy = "first_non_null", name_generator = NULL, upper_bound = 0) {
n_field_strategy = c("first_non_null","max_width"),
name_generator = NULL,
upper_bound = 0
) {
.pr$Expr$list_to_struct(self, n_field_strategy, name_generator, upper_bound) |>
unwrap("in <List>$to_struct():")
}
Expand All @@ -410,7 +416,7 @@ ExprList_to_struct = function(
#' @return Expr
#' @aliases list_eval
#' @examples
#' df = pl$DataFrame(a = list(c(1, 8, 3), b = c(4, 5, 2)))
#' df = pl$DataFrame(a = c(1, 8, 3), b = c(4, 5, 2))
#' df$select(pl$all()$cast(pl$dtypes$Int64))$with_columns(
#' pl$concat_list(c("a", "b"))$list$eval(pl$element()$rank())$alias("rank")
#' )
Expand Down
2 changes: 1 addition & 1 deletion R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ Expr$list_slice <- function(offset, length) .Call(wrap__Expr__list_slice, self,

Expr$list_eval <- function(expr, parallel) .Call(wrap__Expr__list_eval, self, expr, parallel)

Expr$list_to_struct <- function(width_strat, name_gen, upper_bound) .Call(wrap__Expr__list_to_struct, self, width_strat, name_gen, upper_bound)
Expr$list_to_struct <- function(n_field_strategy, name_gen, upper_bound) .Call(wrap__Expr__list_to_struct, self, n_field_strategy, name_gen, upper_bound)

Expr$str_to_date <- function(format, strict, exact, cache, ambiguous) .Call(wrap__Expr__str_to_date, self, format, strict, exact, cache, ambiguous)

Expand Down
14 changes: 3 additions & 11 deletions R/functions__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ pl$approx_n_unique = function(column) { #-> int or Expr
#' df = pl$DataFrame(a = 1:2, b = 3:4, c = 5:6)
#'
#' # Compute sum in several columns
#' df$with_columns(pl$sum("a", "c"))
#' df$with_columns(pl$sum("*"))
pl$sum = function(..., verbose = TRUE) {
column = list2(...)
Expand Down Expand Up @@ -519,9 +518,6 @@ pl$sum = function(..., verbose = TRUE) {
#' )
#' df
#'
#' df$with_columns(
#' pl$min("a", "b", "c")
#' )
pl$min = function(..., verbose = TRUE) {
column = list2(...)
if (length(column) == 1L) column <- column[[1L]]
Expand Down Expand Up @@ -567,9 +563,6 @@ pl$min = function(..., verbose = TRUE) {
#' )
#' df
#'
#' df$with_columns(
#' pl$max("a", "b", "c")
#' )
pl$max = function(..., verbose = TRUE) {
column = list2(...)
if (length(column) == 1L) column <- column[[1L]]
Expand Down Expand Up @@ -667,7 +660,7 @@ pl$var = function(column, ddof = 1) {
#' Concat the arrays in a Series dtype List in linear time.
#' @description Folds the expressions from left to right, keeping the first non-null value.
#' @name pl_concat_list
#' @param exprs list of Expr or Series or strings or a mix, or a char vector
#' @param exprs list of Into<Expr>, strings interpreted as column names
#' @return Expr
#'
#' @keywords Expr_new
Expand All @@ -692,9 +685,8 @@ pl$var = function(column, ddof = 1) {
#' ))$alias("alice")$lit_to_s()
#'
pl$concat_list = function(exprs) {
l_expr = lapply(as.list(exprs), wrap_e)
pra = do.call(construct_ProtoExprArray, l_expr)
unwrap(concat_list(pra))
concat_list(as.list(exprs)) |>
unwrap(" in pl$concat_list():")
}


Expand Down
10 changes: 8 additions & 2 deletions inst/misc/develop_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ find_missing_return = function() {
#' @export
#'
#' @examples
run_all_examples_collect_errors = \(skip_these = character()) {
run_all_examples_collect_errors = \(skip_these = character(), time_examples = FALSE) {
paths = list.files(full.names = TRUE, path = "./man/.")
fnames = list.files(full.names = FALSE, path = "./man/.")
names(paths) = fnames
Expand All @@ -290,10 +290,16 @@ run_all_examples_collect_errors = \(skip_these = character()) {


out = lapply(paths, \(path) {
print(path)
cat("\n",path)
if(time_examples) t1 = Sys.time()
txt = capture.output({
err = polars:::result(pkgload::run_example(path = path))$err
})
if(time_examples) {
t2 = Sys.time()
duration = difftime(t2,t1, units = "secs")
if(duration >.1) cat(" ", duration,"s")
}
if (!is.null(err)) list(err = err, txt = txt)
})

Expand Down
4 changes: 2 additions & 2 deletions man/ExprList_concat.Rd

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

2 changes: 1 addition & 1 deletion man/ExprList_diff.Rd

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

2 changes: 1 addition & 1 deletion man/ExprList_eval.Rd

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

4 changes: 3 additions & 1 deletion man/ExprList_to_struct.Rd

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

2 changes: 1 addition & 1 deletion man/Expr_diff.Rd

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

8 changes: 4 additions & 4 deletions man/nanoarrow.Rd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the wrong update was committed.

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

2 changes: 1 addition & 1 deletion man/pl_concat_list.Rd

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

3 changes: 0 additions & 3 deletions man/pl_max.Rd

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

3 changes: 0 additions & 3 deletions man/pl_min.Rd

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

1 change: 0 additions & 1 deletion man/pl_sum.Rd

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

Loading
Loading