Skip to content

Commit

Permalink
various functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sorhawell committed Nov 18, 2023
1 parent d16d528 commit 2b077b8
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 44 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
`pl$sum_horizontal()` instead for rowwise computation (#508).
- `$is_not()` is deprecated and will be removed in 0.12.0. Use `$not_()` instead
(#511).
- `pl$concat_list()`: elements being strings are now interpreted as column names. Use `pl$lit` to
concat with a string.

## What's changed

Expand Down
8 changes: 4 additions & 4 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#' @useDynLib polars, .registration = TRUE
NULL

min_horizontal <- function(dotdotdot) .Call(wrap__min_horizontal, dotdotdot)

max_horizontal <- function(dotdotdot) .Call(wrap__max_horizontal, dotdotdot)

all_horizontal <- function(dotdotdot) .Call(wrap__all_horizontal, dotdotdot)

any_horizontal <- function(dotdotdot) .Call(wrap__any_horizontal, dotdotdot)

min_horizontal <- function(dotdotdot) .Call(wrap__min_horizontal, dotdotdot)

max_horizontal <- function(dotdotdot) .Call(wrap__max_horizontal, dotdotdot)

sum_horizontal <- function(dotdotdot) .Call(wrap__sum_horizontal, dotdotdot)

coalesce_exprs <- function(exprs) .Call(wrap__coalesce_exprs, exprs)
Expand Down
7 changes: 3 additions & 4 deletions R/functions__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,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 @@ -710,9 +710,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
8 changes: 4 additions & 4 deletions man/nanoarrow.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/pl_concat_list.Rd

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

35 changes: 16 additions & 19 deletions src/rust/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::rdatatype::literal_to_any_value;
use crate::rdatatype::new_null_behavior;
use crate::rdatatype::new_rank_method;
use crate::rdatatype::new_rolling_cov_options;
use crate::rdatatype::parse_fill_null_strategy;
use crate::rdatatype::robj_to_timeunit;
use crate::rdatatype::{DataTypeVector, RPolarsDataType};
use crate::robj_to;
Expand All @@ -11,7 +12,7 @@ use crate::rpolarserr::{rerr, rpolars_to_polars_err, RResult, Rctx, WithRctx};
use crate::series::Series;
use crate::utils::extendr_concurrent::{ParRObj, ThreadCom};
use crate::utils::extendr_helpers::robj_inherits;
use crate::utils::parse_fill_null_strategy;
use crate::utils::robj_to_rchoice;
use crate::utils::wrappers::null_to_opt;
use crate::utils::{r_error_list, r_ok_list, r_result_list, robj_to_binary_vec};
use crate::utils::{
Expand Down Expand Up @@ -324,25 +325,21 @@ impl Expr {
self.0.clone().fill_null(expr.0.clone()).into()
}

pub fn fill_null_with_strategy(&self, strategy: &str, limit: Nullable<f64>) -> List {
let res = || -> Result<Expr, String> {
let limit = null_to_opt(limit).map(try_f64_into_usize).transpose()?;
let limit: pl::FillNullLimit = limit.map(|x| x as u32);

let strat = parse_fill_null_strategy(strategy, limit)
.map_err(|err| format!("this happe4nd {:?}", err))?;
let expr: pl::Expr = self
.0
.clone()
.apply(
move |s| s.fill_null(strat).map(Some),
pl::GetOutput::same_type(),
)
.with_fmt("fill_null_with_strategy");
pub fn fill_null_with_strategy(&self, strategy: Robj, limit: Robj) -> RResult<Self> {
let strat = parse_fill_null_strategy(
robj_to_rchoice(strategy)?.as_str(),
robj_to!(Option, u32, limit)?,
)?;
let expr: pl::Expr = self
.0
.clone()
.apply(
move |s| s.fill_null(strat).map(Some),
pl::GetOutput::same_type(),
)
.with_fmt("fill_null_with_strategy");

Ok(Expr(expr))
}();
r_result_list(res)
Ok(Expr(expr))
}

pub fn fill_nan(&self, expr: &Expr) -> Self {
Expand Down
18 changes: 18 additions & 0 deletions src/rust/src/rdatatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,24 @@ pub fn robj_to_parallel_strategy(robj: extendr_api::Robj) -> RResult<pl::Paralle
}
}

pub fn parse_fill_null_strategy(
strategy: &str,
limit: Option<u32>,
) -> RResult<pl::FillNullStrategy> {
use pl::FillNullStrategy::*;
let parsed = match strategy {
"forward" => Forward(limit),
"backward" => Backward(limit),
"min" => Min,
"max" => Max,
"mean" => Mean,
"zero" => Zero,
"one" => One,
e => return rerr().plain("FillNullStrategy is not known").bad_val(e),
};
Ok(parsed)
}

extendr_module! {
mod rdatatype;
impl RPolarsDataType;
Expand Down
13 changes: 6 additions & 7 deletions src/rust/src/rlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ fn sum_exprs(exprs: &ProtoExprArray) -> Expr {
}

#[extendr]
fn concat_list(exprs: &ProtoExprArray) -> Result<Expr, String> {
let exprs = exprs.to_vec("select");
Ok(Expr(pl::concat_list(exprs).map_err(|err| err.to_string())?))
fn concat_list(exprs: Robj) -> RResult<Expr> {
pl::concat_list(robj_to!(VecPLExprCol, exprs)?)
.map_err(polars_to_rpolars_err)
.map(Expr)
}

#[extendr]
Expand Down Expand Up @@ -89,11 +90,9 @@ fn r_date_range_lazy(
}
}

//TODO py-polars have some fancy transmute conversions TOExprs trait, maybe imple that too
//for now just use inner directly
#[extendr]
fn as_struct(exprs: Robj) -> Result<Expr, String> {
Ok(pl::as_struct(crate::utils::list_expr_to_vec_pl_expr(exprs, true, true)?).into())
fn as_struct(exprs: Robj) -> RResult<Expr> {
Ok(pl::as_struct(robj_to!(VecPLExprNamed, exprs)?).into())
}

#[extendr]
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-expr_arr.R
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ test_that("concat", {
)

expect_identical(
df$select(pl$col("a")$list$concat("hello from R"))$to_list(),
df$select(pl$col("a")$list$concat(pl$lit("hello from R")))$to_list(),
list(a = list(c("a", "hello from R"), c("x", "hello from R")))
)

expect_identical(
df$select(pl$col("a")$list$concat(c("hello", "world")))$to_list(),
df$select(pl$col("a")$list$concat(pl$lit(c("hello", "world"))))$to_list(),
list(a = list(c("a", "hello"), c("x", "world")))
)
})
Expand Down Expand Up @@ -433,15 +433,15 @@ 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_columns(
pl$concat_list(c("a", "b"))$list$eval(pl$element()$rank())$alias("rank")
l_act = df$with_columns(
pl$concat_list(list("a", "b"))$list$eval(pl$element()$rank())$alias("rank")
)$to_list()
expect_identical(
l_act,
list(
a = c(1, 8, 3),
b = c(4, 5, 2),
rank = list(c(1, 2), c(1, 2), c(1, 2))
rank = list(c(1, 2), c(2, 1), c(2, 1))
)
)
})

0 comments on commit 2b077b8

Please sign in to comment.