From 58c88b7e58b02b106082955cfc7b13fb37806728 Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Fri, 23 Aug 2024 21:30:07 +0200 Subject: [PATCH] feat!: remove `$shift_and_fill()`, add `fill_value` to `$shift()` (#1201) --- NEWS.md | 4 + R/dataframe__frame.R | 37 ++--- R/expr__array.R | 6 +- R/expr__expr.R | 27 +--- R/expr__list.R | 8 +- R/extendr-wrappers.R | 8 +- R/group_by.R | 26 +--- R/lazyframe__lazy.R | 31 ++-- man/DataFrame_shift.Rd | 17 ++- man/DataFrame_shift_and_fill.Rd | 31 ---- man/ExprArr_shift.Rd | 6 +- man/ExprList_shift.Rd | 6 +- man/Expr_shift.Rd | 12 +- man/Expr_shift_and_fill.Rd | 26 ---- man/GroupBy_shift.Rd | 13 +- man/GroupBy_shift_and_fill.Rd | 23 --- man/LazyFrame_shift.Rd | 24 ++- man/LazyFrame_shift_and_fill.Rd | 26 ---- src/rust/src/lazy/dataframe.rs | 21 ++- src/rust/src/lazy/dsl.rs | 19 ++- tests/testthat/_snaps/after-wrappers.md | 195 ++++++++++++------------ tests/testthat/test-dataframe.R | 6 +- tests/testthat/test-expr_expr.R | 16 +- tests/testthat/test-groupby.R | 6 +- tests/testthat/test-lazy.R | 6 +- 25 files changed, 234 insertions(+), 366 deletions(-) delete mode 100644 man/DataFrame_shift_and_fill.Rd delete mode 100644 man/Expr_shift_and_fill.Rd delete mode 100644 man/GroupBy_shift_and_fill.Rd delete mode 100644 man/LazyFrame_shift_and_fill.Rd diff --git a/NEWS.md b/NEWS.md index b182dd023..5138ff53e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,6 +25,10 @@ - In `$dt$epoch()`, the argument `tu` is renamed to `time_unit` (#1196). - In `$fill_nan()` for `DataFrame`, `LazyFrame` and `Expr`, the argument is renamed `value` (#1198). +- `$shift_and_fill()` is removed and replaced by a new argument `fill_value` in + `$shift()`. `$shift_and_fill(fill_value, periods)` can be replaced by + `$shift(n, fill_value)` (#1201). +- In `$shift()` for various `Expr`, the argument `periods` is renamed `n` (#1201). ### New features diff --git a/R/dataframe__frame.R b/R/dataframe__frame.R index e296bf007..ab45a516e 100644 --- a/R/dataframe__frame.R +++ b/R/dataframe__frame.R @@ -781,37 +781,22 @@ DataFrame_equals = function(other) { #' rows will be discarded. Vice-versa if the period is negative. In the end, #' the total number of rows of the DataFrame doesn't change. #' -#' @keywords DataFrame -#' @param periods Number of periods to shift (can be negative). -#' @return DataFrame -#' @examples -#' pl$DataFrame(mtcars)$shift(2) -#' -#' pl$DataFrame(mtcars)$shift(-2) -DataFrame_shift = function(periods = 1) { - self$lazy()$shift(periods)$collect() -} - -#' @title Shift and fill +#' @param n Number of indices to shift forward. If a negative value is +#' passed, values are shifted in the opposite direction instead. +#' @param fill_value Fill the resulting null values with this value. Accepts +#' expression input. Non-expression inputs are parsed as literals. #' -#' @description Shift the values by a given period and fill the resulting null -#' values. See the docs of `$shift()` for more details on shifting. -#' @keywords DataFrame -#' -#' @param fill_value Fill new `NULL` values with this value. Must of length 1. -#' A logical value will be converted to numeric. -#' @param periods Number of periods to shift (can be negative). #' @return DataFrame #' @examples -#' df = pl$DataFrame(mtcars) +#' df = pl$DataFrame(a = 1:4, b = 5:8) +#' +#' df$shift(2) #' -#' # insert two rows filled with 0 at the top of the DataFrame -#' df$shift_and_fill(0, 2) +#' df$shift(-2) #' -#' # automatic conversion of logical value to numeric -#' df$shift_and_fill(TRUE, 2) -DataFrame_shift_and_fill = function(fill_value, periods = 1) { - self$lazy()$shift_and_fill(fill_value, periods)$collect() +#' df$shift(-2, fill_value = 100) +DataFrame_shift = function(n = 1, fill_value = NULL) { + self$lazy()$shift(n, fill_value)$collect() } #' Modify/append column(s) diff --git a/R/expr__array.R b/R/expr__array.R index 29df51922..d6dcd443a 100644 --- a/R/expr__array.R +++ b/R/expr__array.R @@ -233,7 +233,7 @@ ExprArr_any = function() .pr$Expr$arr_any(self) #' Shift array values by `n` indices #' -#' @inheritParams ExprList_shift +#' @inheritParams DataFrame_shift #' #' @return Expr #' @examples @@ -246,8 +246,8 @@ ExprArr_any = function() .pr$Expr$arr_any(self) #' shift_by_expr = pl$col("values")$arr$shift(pl$col("idx")), #' shift_by_lit = pl$col("values")$arr$shift(2) #' ) -ExprArr_shift = function(periods = 1) { - .pr$Expr$arr_shift(self, periods) |> +ExprArr_shift = function(n = 1) { + .pr$Expr$arr_shift(self, n) |> unwrap("in $arr$shift():") } diff --git a/R/expr__expr.R b/R/expr__expr.R index dee80c245..2cebce553 100644 --- a/R/expr__expr.R +++ b/R/expr__expr.R @@ -1555,9 +1555,10 @@ Expr_gather = function(indices) { unwrap("in $gather():") } -#' Shift values +#' Shift values by the given number of indices +#' +#' @inheritParams DataFrame_shift #' -#' @param periods Number of periods to shift, may be negative. #' @return Expr #' @examples #' pl$DataFrame(a = c(1, 2, 4, 5, 8))$ @@ -1565,29 +1566,11 @@ Expr_gather = function(indices) { #' pl$col("a")$shift(-2)$alias("shift-2"), #' pl$col("a")$shift(2)$alias("shift+2") #' ) -Expr_shift = function(periods = 1) { - .pr$Expr$shift(self, periods) |> +Expr_shift = function(n = 1, fill_value = NULL) { + .pr$Expr$shift(self, n, fill_value) |> unwrap("in $shift():") } -#' Shift and fill values -#' -#' Shift the values by a given period and fill the resulting null values. -#' -#' @inheritParams Expr_shift -#' @param fill_value Fill null values with the result of this expression. -#' @return Expr -#' @examples -#' pl$DataFrame(a = c(1, 2, 4, 5, 8))$ -#' with_columns( -#' pl$col("a")$shift_and_fill(-2, fill_value = 42)$alias("shift-2"), -#' pl$col("a")$shift_and_fill(2, fill_value = pl$col("a") / 2)$alias("shift+2") -#' ) -Expr_shift_and_fill = function(periods, fill_value) { - .pr$Expr$shift_and_fill(self, periods, pl$lit(fill_value)) |> - unwrap("in $shift_and_fill():") -} - #' Fill null values with a value or strategy #' #' @param value Expr or something coercible in an Expr diff --git a/R/expr__list.R b/R/expr__list.R index 3c9c1d9a4..eb3f23c36 100644 --- a/R/expr__list.R +++ b/R/expr__list.R @@ -311,8 +311,7 @@ ExprList_diff = function(n = 1, null_behavior = c("ignore", "drop")) { #' Shift list values by `n` indices #' -#' @param periods Number of places to shift (may be negative). Can be an Expr. -#' Strings are *not* parsed as columns. +#' @inheritParams DataFrame_shift #' #' @return Expr #' @@ -325,7 +324,10 @@ ExprList_diff = function(n = 1, null_behavior = c("ignore", "drop")) { #' shift_by_expr = pl$col("s")$list$shift(pl$col("idx")), #' shift_by_lit = pl$col("s")$list$shift(2) #' ) -ExprList_shift = function(periods = 1) unwrap(.pr$Expr$list_shift(self, periods)) +ExprList_shift = function(n = 1) { + .pr$Expr$list_shift(self, n) |> + unwrap("in $list$shift():") +} #' Slice list #' diff --git a/R/extendr-wrappers.R b/R/extendr-wrappers.R index 266812325..87a6b28d6 100644 --- a/R/extendr-wrappers.R +++ b/R/extendr-wrappers.R @@ -548,9 +548,7 @@ RPolarsExpr$backward_fill <- function(limit) .Call(wrap__RPolarsExpr__backward_f RPolarsExpr$forward_fill <- function(limit) .Call(wrap__RPolarsExpr__forward_fill, self, limit) -RPolarsExpr$shift <- function(periods) .Call(wrap__RPolarsExpr__shift, self, periods) - -RPolarsExpr$shift_and_fill <- function(periods, fill_value) .Call(wrap__RPolarsExpr__shift_and_fill, self, periods, fill_value) +RPolarsExpr$shift <- function(n, fill_value) .Call(wrap__RPolarsExpr__shift, self, n, fill_value) RPolarsExpr$fill_null <- function(expr) .Call(wrap__RPolarsExpr__fill_null, self, expr) @@ -1220,9 +1218,7 @@ RPolarsLazyFrame$var <- function(ddof) .Call(wrap__RPolarsLazyFrame__var, self, RPolarsLazyFrame$quantile <- function(quantile, interpolation) .Call(wrap__RPolarsLazyFrame__quantile, self, quantile, interpolation) -RPolarsLazyFrame$shift <- function(periods) .Call(wrap__RPolarsLazyFrame__shift, self, periods) - -RPolarsLazyFrame$shift_and_fill <- function(fill_value, periods) .Call(wrap__RPolarsLazyFrame__shift_and_fill, self, fill_value, periods) +RPolarsLazyFrame$shift <- function(n, fill_value) .Call(wrap__RPolarsLazyFrame__shift, self, n, fill_value) RPolarsLazyFrame$reverse <- function() .Call(wrap__RPolarsLazyFrame__reverse, self) diff --git a/R/group_by.R b/R/group_by.R index 8710b5b05..8e80505da 100644 --- a/R/group_by.R +++ b/R/group_by.R @@ -272,25 +272,15 @@ GroupBy_quantile = function(quantile, interpolation = "nearest") { self$agg(pl$all()$quantile(quantile, interpolation)) } -#' @title Shift -#' @description Shift the values by a given period. -#' @keywords GroupBy -#' @param periods integer Number of periods to shift (may be negative). -#' @return GroupBy -#' @examples pl$DataFrame(mtcars)$group_by("cyl")$shift(2) -GroupBy_shift = function(periods = 1) { - self$agg(pl$all()$shift(periods)) -} - -#' @title Shift and fill -#' @description Shift and fill the values by a given period. -#' @keywords GroupBy -#' @param fill_value fill None values with the result of this expression. -#' @param periods integer Number of periods to shift (may be negative). +#' Shift the values by a given period +#' +#' @inheritParams DataFrame_shift +#' #' @return GroupBy -#' @examples pl$DataFrame(mtcars)$group_by("cyl")$shift_and_fill(99, 1) -GroupBy_shift_and_fill = function(fill_value, periods = 1) { - self$agg(pl$all()$shift_and_fill(periods, fill_value)) +#' @examples +#' pl$DataFrame(mtcars)$group_by("cyl")$shift(2) +GroupBy_shift = function(n = 1, fill_value = NULL) { + self$agg(pl$all()$shift(n, fill_value)) } #' @title GroupBy null count diff --git a/R/lazyframe__lazy.R b/R/lazyframe__lazy.R index e95933d0b..312721688 100644 --- a/R/lazyframe__lazy.R +++ b/R/lazyframe__lazy.R @@ -1104,27 +1104,22 @@ LazyFrame_fill_null = function(fill_value) { unwrap(.pr$LazyFrame$fill_null(self, wrap_e_result(fill_value)), "in $fill_null():") } -#' @title Shift -#' @description Shift the values by a given period. -#' @keywords LazyFrame -#' @param periods integer Number of periods to shift (may be negative). -#' @return LazyFrame -#' @examples pl$LazyFrame(mtcars)$shift(2)$collect() -LazyFrame_shift = function(periods = 1) { - unwrap(.pr$LazyFrame$shift(self, periods), "in $shift():") -} - #' Shift a LazyFrame #' -#' @description Shift the values by a given period. If the period (`n`) is positive, -#' then `n` rows will be inserted at the top of the LazyFrame and the last `n` -#' rows will be discarded. Vice-versa if the period is negative. In the end, -#' the total number of rows of the LazyFrame doesn't change. -#' @inheritParams DataFrame_shift_and_fill +#' @inherit DataFrame_shift description params +#' #' @return LazyFrame -#' @examples pl$LazyFrame(mtcars)$shift_and_fill(0., 2.)$collect()$to_data_frame() -LazyFrame_shift_and_fill = function(fill_value, periods = 1) { - unwrap(.pr$LazyFrame$shift_and_fill(self, wrap_e(fill_value), periods), "in $shift_and_fill():") +#' @examples +#' lf = pl$LazyFrame(a = 1:4, b = 5:8) +#' +#' lf$shift(2)$collect() +#' +#' lf$shift(-2)$collect() +#' +#' lf$shift(-2, fill_value = 100)$collect() +LazyFrame_shift = function(n = 1, fill_value = NULL) { + .pr$LazyFrame$shift(self, n, fill_value) |> + unwrap("in $shift():") } #' Drop columns of a LazyFrame diff --git a/man/DataFrame_shift.Rd b/man/DataFrame_shift.Rd index 7b7eee7c8..0fb4d3946 100644 --- a/man/DataFrame_shift.Rd +++ b/man/DataFrame_shift.Rd @@ -4,10 +4,14 @@ \alias{DataFrame_shift} \title{Shift a DataFrame} \usage{ -DataFrame_shift(periods = 1) +DataFrame_shift(n = 1, fill_value = NULL) } \arguments{ -\item{periods}{Number of periods to shift (can be negative).} +\item{n}{Number of indices to shift forward. If a negative value is +passed, values are shifted in the opposite direction instead.} + +\item{fill_value}{Fill the resulting null values with this value. Accepts +expression input. Non-expression inputs are parsed as literals.} } \value{ DataFrame @@ -19,8 +23,11 @@ rows will be discarded. Vice-versa if the period is negative. In the end, the total number of rows of the DataFrame doesn't change. } \examples{ -pl$DataFrame(mtcars)$shift(2) +df = pl$DataFrame(a = 1:4, b = 5:8) + +df$shift(2) + +df$shift(-2) -pl$DataFrame(mtcars)$shift(-2) +df$shift(-2, fill_value = 100) } -\keyword{DataFrame} diff --git a/man/DataFrame_shift_and_fill.Rd b/man/DataFrame_shift_and_fill.Rd deleted file mode 100644 index 5453eb5ac..000000000 --- a/man/DataFrame_shift_and_fill.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/dataframe__frame.R -\name{DataFrame_shift_and_fill} -\alias{DataFrame_shift_and_fill} -\title{Shift and fill} -\usage{ -DataFrame_shift_and_fill(fill_value, periods = 1) -} -\arguments{ -\item{fill_value}{Fill new \code{NULL} values with this value. Must of length 1. -A logical value will be converted to numeric.} - -\item{periods}{Number of periods to shift (can be negative).} -} -\value{ -DataFrame -} -\description{ -Shift the values by a given period and fill the resulting null -values. See the docs of \verb{$shift()} for more details on shifting. -} -\examples{ -df = pl$DataFrame(mtcars) - -# insert two rows filled with 0 at the top of the DataFrame -df$shift_and_fill(0, 2) - -# automatic conversion of logical value to numeric -df$shift_and_fill(TRUE, 2) -} -\keyword{DataFrame} diff --git a/man/ExprArr_shift.Rd b/man/ExprArr_shift.Rd index ffa390d00..49ba81e43 100644 --- a/man/ExprArr_shift.Rd +++ b/man/ExprArr_shift.Rd @@ -4,11 +4,11 @@ \alias{ExprArr_shift} \title{Shift array values by \code{n} indices} \usage{ -ExprArr_shift(periods = 1) +ExprArr_shift(n = 1) } \arguments{ -\item{periods}{Number of places to shift (may be negative). Can be an Expr. -Strings are \emph{not} parsed as columns.} +\item{n}{Number of indices to shift forward. If a negative value is +passed, values are shifted in the opposite direction instead.} } \value{ Expr diff --git a/man/ExprList_shift.Rd b/man/ExprList_shift.Rd index 67f8bc9cc..726eb6f89 100644 --- a/man/ExprList_shift.Rd +++ b/man/ExprList_shift.Rd @@ -4,11 +4,11 @@ \alias{ExprList_shift} \title{Shift list values by \code{n} indices} \usage{ -ExprList_shift(periods = 1) +ExprList_shift(n = 1) } \arguments{ -\item{periods}{Number of places to shift (may be negative). Can be an Expr. -Strings are \emph{not} parsed as columns.} +\item{n}{Number of indices to shift forward. If a negative value is +passed, values are shifted in the opposite direction instead.} } \value{ Expr diff --git a/man/Expr_shift.Rd b/man/Expr_shift.Rd index 963e6c771..14e533870 100644 --- a/man/Expr_shift.Rd +++ b/man/Expr_shift.Rd @@ -2,18 +2,22 @@ % Please edit documentation in R/expr__expr.R \name{Expr_shift} \alias{Expr_shift} -\title{Shift values} +\title{Shift values by the given number of indices} \usage{ -Expr_shift(periods = 1) +Expr_shift(n = 1, fill_value = NULL) } \arguments{ -\item{periods}{Number of periods to shift, may be negative.} +\item{n}{Number of indices to shift forward. If a negative value is +passed, values are shifted in the opposite direction instead.} + +\item{fill_value}{Fill the resulting null values with this value. Accepts +expression input. Non-expression inputs are parsed as literals.} } \value{ Expr } \description{ -Shift values +Shift values by the given number of indices } \examples{ pl$DataFrame(a = c(1, 2, 4, 5, 8))$ diff --git a/man/Expr_shift_and_fill.Rd b/man/Expr_shift_and_fill.Rd deleted file mode 100644 index e9fed01e0..000000000 --- a/man/Expr_shift_and_fill.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/expr__expr.R -\name{Expr_shift_and_fill} -\alias{Expr_shift_and_fill} -\title{Shift and fill values} -\usage{ -Expr_shift_and_fill(periods, fill_value) -} -\arguments{ -\item{periods}{Number of periods to shift, may be negative.} - -\item{fill_value}{Fill null values with the result of this expression.} -} -\value{ -Expr -} -\description{ -Shift the values by a given period and fill the resulting null values. -} -\examples{ -pl$DataFrame(a = c(1, 2, 4, 5, 8))$ - with_columns( - pl$col("a")$shift_and_fill(-2, fill_value = 42)$alias("shift-2"), - pl$col("a")$shift_and_fill(2, fill_value = pl$col("a") / 2)$alias("shift+2") -) -} diff --git a/man/GroupBy_shift.Rd b/man/GroupBy_shift.Rd index 605bf05c2..1098bfcc3 100644 --- a/man/GroupBy_shift.Rd +++ b/man/GroupBy_shift.Rd @@ -2,20 +2,23 @@ % Please edit documentation in R/group_by.R \name{GroupBy_shift} \alias{GroupBy_shift} -\title{Shift} +\title{Shift the values by a given period} \usage{ -GroupBy_shift(periods = 1) +GroupBy_shift(n = 1, fill_value = NULL) } \arguments{ -\item{periods}{integer Number of periods to shift (may be negative).} +\item{n}{Number of indices to shift forward. If a negative value is +passed, values are shifted in the opposite direction instead.} + +\item{fill_value}{Fill the resulting null values with this value. Accepts +expression input. Non-expression inputs are parsed as literals.} } \value{ GroupBy } \description{ -Shift the values by a given period. +Shift the values by a given period } \examples{ pl$DataFrame(mtcars)$group_by("cyl")$shift(2) } -\keyword{GroupBy} diff --git a/man/GroupBy_shift_and_fill.Rd b/man/GroupBy_shift_and_fill.Rd deleted file mode 100644 index b51b07984..000000000 --- a/man/GroupBy_shift_and_fill.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/group_by.R -\name{GroupBy_shift_and_fill} -\alias{GroupBy_shift_and_fill} -\title{Shift and fill} -\usage{ -GroupBy_shift_and_fill(fill_value, periods = 1) -} -\arguments{ -\item{fill_value}{fill None values with the result of this expression.} - -\item{periods}{integer Number of periods to shift (may be negative).} -} -\value{ -GroupBy -} -\description{ -Shift and fill the values by a given period. -} -\examples{ -pl$DataFrame(mtcars)$group_by("cyl")$shift_and_fill(99, 1) -} -\keyword{GroupBy} diff --git a/man/LazyFrame_shift.Rd b/man/LazyFrame_shift.Rd index 3d2c6431b..8d842d3cc 100644 --- a/man/LazyFrame_shift.Rd +++ b/man/LazyFrame_shift.Rd @@ -2,20 +2,32 @@ % Please edit documentation in R/lazyframe__lazy.R \name{LazyFrame_shift} \alias{LazyFrame_shift} -\title{Shift} +\title{Shift a LazyFrame} \usage{ -LazyFrame_shift(periods = 1) +LazyFrame_shift(n = 1, fill_value = NULL) } \arguments{ -\item{periods}{integer Number of periods to shift (may be negative).} +\item{n}{Number of indices to shift forward. If a negative value is +passed, values are shifted in the opposite direction instead.} + +\item{fill_value}{Fill the resulting null values with this value. Accepts +expression input. Non-expression inputs are parsed as literals.} } \value{ LazyFrame } \description{ -Shift the values by a given period. +Shift the values by a given period. If the period (\code{n}) is positive, +then \code{n} rows will be inserted at the top of the DataFrame and the last \code{n} +rows will be discarded. Vice-versa if the period is negative. In the end, +the total number of rows of the DataFrame doesn't change. } \examples{ -pl$LazyFrame(mtcars)$shift(2)$collect() +lf = pl$LazyFrame(a = 1:4, b = 5:8) + +lf$shift(2)$collect() + +lf$shift(-2)$collect() + +lf$shift(-2, fill_value = 100)$collect() } -\keyword{LazyFrame} diff --git a/man/LazyFrame_shift_and_fill.Rd b/man/LazyFrame_shift_and_fill.Rd deleted file mode 100644 index 7c9c9ff0b..000000000 --- a/man/LazyFrame_shift_and_fill.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/lazyframe__lazy.R -\name{LazyFrame_shift_and_fill} -\alias{LazyFrame_shift_and_fill} -\title{Shift a LazyFrame} -\usage{ -LazyFrame_shift_and_fill(fill_value, periods = 1) -} -\arguments{ -\item{fill_value}{Fill new \code{NULL} values with this value. Must of length 1. -A logical value will be converted to numeric.} - -\item{periods}{Number of periods to shift (can be negative).} -} -\value{ -LazyFrame -} -\description{ -Shift the values by a given period. If the period (\code{n}) is positive, -then \code{n} rows will be inserted at the top of the LazyFrame and the last \code{n} -rows will be discarded. Vice-versa if the period is negative. In the end, -the total number of rows of the LazyFrame doesn't change. -} -\examples{ -pl$LazyFrame(mtcars)$shift_and_fill(0., 2.)$collect()$to_data_frame() -} diff --git a/src/rust/src/lazy/dataframe.rs b/src/rust/src/lazy/dataframe.rs index 62aef57b2..e0dfa4f84 100644 --- a/src/rust/src/lazy/dataframe.rs +++ b/src/rust/src/lazy/dataframe.rs @@ -11,7 +11,7 @@ use crate::utils::try_f64_into_usize; use extendr_api::prelude::*; use pl::{AsOfOptions, Duration, RollingGroupOptions}; use polars::chunked_array::ops::SortMultipleOptions; -use polars::prelude as pl; +use polars::prelude::{self as pl}; use polars::prelude::{JoinCoalesce, SerializeOptions, UnpivotArgsDSL}; use polars_lazy::prelude::CsvWriterOptions; @@ -254,16 +254,15 @@ impl RPolarsLazyFrame { Ok(out.into()) } - fn shift(&self, periods: Robj) -> RResult { - Ok(self.clone().0.shift(robj_to!(i64, periods)?).into()) - } - - fn shift_and_fill(&self, fill_value: Robj, periods: Robj) -> RResult { - Ok(self - .clone() - .0 - .shift_and_fill(robj_to!(PLExpr, periods)?, robj_to!(PLExpr, fill_value)?) - .into()) + fn shift(&self, n: Robj, fill_value: Robj) -> RResult { + let lf = self.0.clone(); + let n = robj_to!(PLExpr, n)?; + let fill_value = robj_to!(Option, PLExpr, fill_value)?; + let out = match fill_value { + Some(v) => lf.shift_and_fill(n, v), + None => lf.shift(n), + }; + Ok(out.into()) } fn reverse(&self) -> Self { diff --git a/src/rust/src/lazy/dsl.rs b/src/rust/src/lazy/dsl.rs index 1e0f1bcc8..82a4b6464 100644 --- a/src/rust/src/lazy/dsl.rs +++ b/src/rust/src/lazy/dsl.rs @@ -327,16 +327,15 @@ impl RPolarsExpr { self.clone().0.forward_fill(lmt).into() } - pub fn shift(&self, periods: Robj) -> RResult { - Ok(self.clone().0.shift(robj_to!(PLExpr, periods)?).into()) - } - - pub fn shift_and_fill(&self, periods: Robj, fill_value: Robj) -> RResult { - Ok(self - .0 - .clone() - .shift_and_fill(robj_to!(PLExpr, periods)?, robj_to!(PLExpr, fill_value)?) - .into()) + pub fn shift(&self, n: Robj, fill_value: Robj) -> RResult { + let expr = self.0.clone(); + let n = robj_to!(PLExpr, n)?; + let fill_value = robj_to!(Option, PLExpr, fill_value)?; + let out = match fill_value { + Some(v) => expr.shift_and_fill(n, v), + None => expr.shift(n), + }; + Ok(out.into()) } pub fn fill_null(&self, expr: Robj) -> RResult { diff --git a/tests/testthat/_snaps/after-wrappers.md b/tests/testthat/_snaps/after-wrappers.md index d98e6e32c..b6c23220d 100644 --- a/tests/testthat/_snaps/after-wrappers.md +++ b/tests/testthat/_snaps/after-wrappers.md @@ -89,24 +89,22 @@ [37] "null_count" "partition_by" "pivot" "print" [41] "quantile" "rechunk" "rename" "reverse" [45] "rolling" "sample" "schema" "select" - [49] "select_seq" "shape" "shift" "shift_and_fill" - [53] "slice" "sort" "sql" "std" - [57] "sum" "tail" "to_data_frame" "to_list" - [61] "to_raw_ipc" "to_series" "to_struct" "transpose" - [65] "unique" "unnest" "unpivot" "var" - [69] "width" "with_columns" "with_columns_seq" "with_row_index" - [73] "write_csv" "write_ipc" "write_json" "write_ndjson" - [77] "write_parquet" + [49] "select_seq" "shape" "shift" "slice" + [53] "sort" "sql" "std" "sum" + [57] "tail" "to_data_frame" "to_list" "to_raw_ipc" + [61] "to_series" "to_struct" "transpose" "unique" + [65] "unnest" "unpivot" "var" "width" + [69] "with_columns" "with_columns_seq" "with_row_index" "write_csv" + [73] "write_ipc" "write_json" "write_ndjson" "write_parquet" # public and private methods of each class GroupBy Code ls(.pr$env[[class_name]]) Output - [1] "agg" "columns" "first" "last" - [5] "max" "mean" "median" "min" - [9] "null_count" "quantile" "shift" "shift_and_fill" - [13] "std" "sum" "ungroup" "var" + [1] "agg" "columns" "first" "last" "max" + [6] "mean" "median" "min" "null_count" "quantile" + [11] "shift" "std" "sum" "ungroup" "var" # public and private methods of each class LazyFrame @@ -125,14 +123,14 @@ [28] "print" "profile" "quantile" [31] "rename" "reverse" "rolling" [34] "schema" "select" "select_seq" - [37] "serialize" "shift" "shift_and_fill" - [40] "sink_csv" "sink_ipc" "sink_ndjson" - [43] "sink_parquet" "slice" "sort" - [46] "sql" "std" "sum" - [49] "tail" "to_dot" "unique" - [52] "unnest" "unpivot" "var" - [55] "width" "with_columns" "with_columns_seq" - [58] "with_context" "with_row_index" + [37] "serialize" "shift" "sink_csv" + [40] "sink_ipc" "sink_ndjson" "sink_parquet" + [43] "slice" "sort" "sql" + [46] "std" "sum" "tail" + [49] "to_dot" "unique" "unnest" + [52] "unpivot" "var" "width" + [55] "with_columns" "with_columns_seq" "with_context" + [58] "with_row_index" # public and private methods of each class Expr @@ -191,16 +189,16 @@ [148] "rolling_std_by" "rolling_sum" "rolling_sum_by" [151] "rolling_var" "rolling_var_by" "round" [154] "sample" "search_sorted" "set_sorted" - [157] "shift" "shift_and_fill" "shrink_dtype" - [160] "shuffle" "sign" "sin" - [163] "sinh" "skew" "slice" - [166] "sort" "sort_by" "sqrt" - [169] "std" "str" "struct" - [172] "sub" "sum" "tail" - [175] "tan" "tanh" "to_physical" - [178] "to_r" "to_series" "top_k" - [181] "unique" "unique_counts" "upper_bound" - [184] "value_counts" "var" "xor" + [157] "shift" "shrink_dtype" "shuffle" + [160] "sign" "sin" "sinh" + [163] "skew" "slice" "sort" + [166] "sort_by" "sqrt" "std" + [169] "str" "struct" "sub" + [172] "sum" "tail" "tan" + [175] "tanh" "to_physical" "to_r" + [178] "to_series" "top_k" "unique" + [181] "unique_counts" "upper_bound" "value_counts" + [184] "var" "xor" --- @@ -340,41 +338,41 @@ [261] "rolling_var_by" "round" [263] "sample_frac" "sample_n" [265] "search_sorted" "shift" - [267] "shift_and_fill" "shrink_dtype" - [269] "shuffle" "sign" - [271] "sin" "sinh" - [273] "skew" "slice" - [275] "sort_by" "sort_with" - [277] "std" "str_base64_decode" - [279] "str_base64_encode" "str_contains" - [281] "str_contains_any" "str_count_matches" - [283] "str_ends_with" "str_extract" - [285] "str_extract_all" "str_extract_groups" - [287] "str_extract_many" "str_find" - [289] "str_head" "str_hex_decode" - [291] "str_hex_encode" "str_join" - [293] "str_json_decode" "str_json_path_match" - [295] "str_len_bytes" "str_len_chars" - [297] "str_pad_end" "str_pad_start" - [299] "str_replace" "str_replace_all" - [301] "str_replace_many" "str_reverse" - [303] "str_slice" "str_split" - [305] "str_split_exact" "str_splitn" - [307] "str_starts_with" "str_strip_chars" - [309] "str_strip_chars_end" "str_strip_chars_start" - [311] "str_tail" "str_to_date" - [313] "str_to_datetime" "str_to_integer" - [315] "str_to_lowercase" "str_to_time" - [317] "str_to_titlecase" "str_to_uppercase" - [319] "str_zfill" "struct_field_by_name" - [321] "struct_rename_fields" "struct_with_fields" - [323] "sub" "sum" - [325] "tail" "tan" - [327] "tanh" "to_physical" - [329] "top_k" "unique" - [331] "unique_counts" "unique_stable" - [333] "upper_bound" "value_counts" - [335] "var" "xor" + [267] "shrink_dtype" "shuffle" + [269] "sign" "sin" + [271] "sinh" "skew" + [273] "slice" "sort_by" + [275] "sort_with" "std" + [277] "str_base64_decode" "str_base64_encode" + [279] "str_contains" "str_contains_any" + [281] "str_count_matches" "str_ends_with" + [283] "str_extract" "str_extract_all" + [285] "str_extract_groups" "str_extract_many" + [287] "str_find" "str_head" + [289] "str_hex_decode" "str_hex_encode" + [291] "str_join" "str_json_decode" + [293] "str_json_path_match" "str_len_bytes" + [295] "str_len_chars" "str_pad_end" + [297] "str_pad_start" "str_replace" + [299] "str_replace_all" "str_replace_many" + [301] "str_reverse" "str_slice" + [303] "str_split" "str_split_exact" + [305] "str_splitn" "str_starts_with" + [307] "str_strip_chars" "str_strip_chars_end" + [309] "str_strip_chars_start" "str_tail" + [311] "str_to_date" "str_to_datetime" + [313] "str_to_integer" "str_to_lowercase" + [315] "str_to_time" "str_to_titlecase" + [317] "str_to_uppercase" "str_zfill" + [319] "struct_field_by_name" "struct_rename_fields" + [321] "struct_with_fields" "sub" + [323] "sum" "tail" + [325] "tan" "tanh" + [327] "to_physical" "top_k" + [329] "unique" "unique_counts" + [331] "unique_stable" "upper_bound" + [333] "value_counts" "var" + [335] "xor" # public and private methods of each class When @@ -447,17 +445,17 @@ [148] "rolling_std" "rolling_std_by" "rolling_sum" [151] "rolling_sum_by" "rolling_var" "rolling_var_by" [154] "round" "sample" "search_sorted" - [157] "set_sorted" "shift" "shift_and_fill" - [160] "shrink_dtype" "shuffle" "sign" - [163] "sin" "sinh" "skew" - [166] "slice" "sort" "sort_by" - [169] "sqrt" "std" "str" - [172] "struct" "sub" "sum" - [175] "tail" "tan" "tanh" - [178] "to_physical" "to_r" "to_series" - [181] "top_k" "unique" "unique_counts" - [184] "upper_bound" "value_counts" "var" - [187] "when" "xor" + [157] "set_sorted" "shift" "shrink_dtype" + [160] "shuffle" "sign" "sin" + [163] "sinh" "skew" "slice" + [166] "sort" "sort_by" "sqrt" + [169] "std" "str" "struct" + [172] "sub" "sum" "tail" + [175] "tan" "tanh" "to_physical" + [178] "to_r" "to_series" "top_k" + [181] "unique" "unique_counts" "upper_bound" + [184] "value_counts" "var" "when" + [187] "xor" --- @@ -537,17 +535,17 @@ [148] "rolling_std" "rolling_std_by" "rolling_sum" [151] "rolling_sum_by" "rolling_var" "rolling_var_by" [154] "round" "sample" "search_sorted" - [157] "set_sorted" "shift" "shift_and_fill" - [160] "shrink_dtype" "shuffle" "sign" - [163] "sin" "sinh" "skew" - [166] "slice" "sort" "sort_by" - [169] "sqrt" "std" "str" - [172] "struct" "sub" "sum" - [175] "tail" "tan" "tanh" - [178] "to_physical" "to_r" "to_series" - [181] "top_k" "unique" "unique_counts" - [184] "upper_bound" "value_counts" "var" - [187] "when" "xor" + [157] "set_sorted" "shift" "shrink_dtype" + [160] "shuffle" "sign" "sin" + [163] "sinh" "skew" "slice" + [166] "sort" "sort_by" "sqrt" + [169] "std" "str" "struct" + [172] "sub" "sum" "tail" + [175] "tan" "tanh" "to_physical" + [178] "to_r" "to_series" "top_k" + [181] "unique" "unique_counts" "upper_bound" + [184] "value_counts" "var" "when" + [187] "xor" --- @@ -630,18 +628,17 @@ [154] "rolling_std_by" "rolling_sum" "rolling_sum_by" [157] "rolling_var" "rolling_var_by" "round" [160] "sample" "search_sorted" "set_sorted" - [163] "shape" "shift" "shift_and_fill" - [166] "shrink_dtype" "shuffle" "sign" - [169] "sin" "sinh" "skew" - [172] "slice" "sort" "sort_by" - [175] "sqrt" "std" "str" - [178] "struct" "sub" "sum" - [181] "tail" "tan" "tanh" - [184] "to_frame" "to_list" "to_lit" - [187] "to_physical" "to_r" "to_vector" - [190] "top_k" "unique" "unique_counts" - [193] "upper_bound" "value_counts" "var" - [196] "xor" + [163] "shape" "shift" "shrink_dtype" + [166] "shuffle" "sign" "sin" + [169] "sinh" "skew" "slice" + [172] "sort" "sort_by" "sqrt" + [175] "std" "str" "struct" + [178] "sub" "sum" "tail" + [181] "tan" "tanh" "to_frame" + [184] "to_list" "to_lit" "to_physical" + [187] "to_r" "to_vector" "top_k" + [190] "unique" "unique_counts" "upper_bound" + [193] "value_counts" "var" "xor" --- diff --git a/tests/testthat/test-dataframe.R b/tests/testthat/test-dataframe.R index 9a133a889..0538f035a 100644 --- a/tests/testthat/test-dataframe.R +++ b/tests/testthat/test-dataframe.R @@ -708,12 +708,12 @@ test_that("drop_in_place", { }) -test_that("shift _and_fill", { - a = pl$DataFrame(mtcars)$shift(2)$head(3)$to_data_frame() +test_that("shift", { + a = pl$DataFrame(mtcars[1:3, ])$shift(2)$to_data_frame() for (i in seq_along(a)) { expect_equal(is.na(a[[i]]), c(TRUE, TRUE, FALSE)) } - a = pl$DataFrame(mtcars)$shift_and_fill(0., 2.)$head(3)$to_data_frame() + a = pl$DataFrame(mtcars[1:3, ])$shift(2, 0)$to_data_frame() for (i in seq_along(a)) { expect_equal(a[[i]], c(0, 0, mtcars[[i]][1])) } diff --git a/tests/testthat/test-expr_expr.R b/tests/testthat/test-expr_expr.R index 510140de2..4972a6164 100644 --- a/tests/testthat/test-expr_expr.R +++ b/tests/testthat/test-expr_expr.R @@ -1068,8 +1068,8 @@ test_that("shift", { expect_identical( pl$select( - pl$lit(0:3)$shift_and_fill(-2, fill_value = 42)$alias("sm2"), - pl$lit(0:3)$shift_and_fill(2, fill_value = pl$lit(42) / 2)$alias("sp2") + pl$lit(0:3)$shift(-2, fill_value = 42)$alias("sm2"), + pl$lit(0:3)$shift(2, fill_value = pl$lit(42) / 2)$alias("sp2") )$to_list(), list( sm2 = R_shift_and_fill(0:3, -2, 42), @@ -1998,15 +1998,15 @@ test_that("kurtosis", { expect_equal( pl$DataFrame(l2)$select( pl$col("a")$kurtosis()$alias("kurt_TT"), - pl$col("a")$kurtosis(fisher = TRUE, bias=FALSE)$alias("kurt_TF"), + pl$col("a")$kurtosis(fisher = TRUE, bias = FALSE)$alias("kurt_TF"), pl$col("a")$kurtosis(fisher = FALSE, bias = TRUE)$alias("kurt_FT"), - pl$col("a")$kurtosis(fisher = FALSE, bias= FALSE)$alias("kurt_FF") + pl$col("a")$kurtosis(fisher = FALSE, bias = FALSE)$alias("kurt_FF") )$to_list(), list2( - kurt_TT = R_kurtosis(l2$a, TRUE, TRUE), - kurt_TF = R_kurtosis(l2$a, TRUE, FALSE), - kurt_FT = R_kurtosis(l2$a, FALSE, TRUE), - kurt_FF = R_kurtosis(l2$a, FALSE, FALSE) + kurt_TT = R_kurtosis(l2$a, TRUE, TRUE), + kurt_TF = R_kurtosis(l2$a, TRUE, FALSE), + kurt_FT = R_kurtosis(l2$a, FALSE, TRUE), + kurt_FF = R_kurtosis(l2$a, FALSE, FALSE) ) ) }) diff --git a/tests/testthat/test-groupby.R b/tests/testthat/test-groupby.R index c853152ea..4f52aa4f0 100644 --- a/tests/testthat/test-groupby.R +++ b/tests/testthat/test-groupby.R @@ -85,16 +85,14 @@ test_that("quantile", { expect_equal(a[order(a$cyl), ], b[order(b$cyl), ], ignore_attr = TRUE) }) -test_that("shift _and_fill", { +test_that("shift", { a = pl$DataFrame(mtcars)$group_by("cyl")$shift(2)$to_data_frame() expect_equal(a[["mpg"]][[1]][1:2], c(NA_real_, NA_real_)) - a = pl$DataFrame(mtcars)$group_by("cyl")$shift_and_fill(99, 2)$to_data_frame() + a = pl$DataFrame(mtcars)$group_by("cyl")$shift(2, 99)$to_data_frame() expect_equal(a[["mpg"]][[1]][1:2], c(99, 99)) }) - - test_that("groupby, lazygroupby unpack + charvec same as list of strings", { skip_if_not_installed("withr") withr::with_options( diff --git a/tests/testthat/test-lazy.R b/tests/testthat/test-lazy.R index 1902cd579..6b12643b9 100644 --- a/tests/testthat/test-lazy.R +++ b/tests/testthat/test-lazy.R @@ -232,12 +232,12 @@ test_that("tail", { }) -test_that("shift _and_fill", { - a = pl$DataFrame(mtcars)$lazy()$shift(2)$limit(3)$collect()$to_data_frame() +test_that("shift", { + a = pl$LazyFrame(mtcars[1:3, ])$shift(2)$collect()$to_data_frame() for (i in seq_along(a)) { expect_equal(is.na(a[[i]]), c(TRUE, TRUE, FALSE)) } - a = pl$DataFrame(mtcars)$lazy()$shift_and_fill(0., 2.)$limit(3)$collect()$to_data_frame() + a = pl$LazyFrame(mtcars[1:3, ])$shift(2, 0)$collect()$to_data_frame() for (i in seq_along(a)) { expect_equal(a[[i]], c(0, 0, mtcars[[i]][1])) }