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

Rename argument drop_nulls to ignore_nulls in $all() and $any() #1050

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## polars (development version)

### Breaking changes

- In `$all()` and `$any()`, the argument `drop_nulls` is renamed `ignore_nulls`
(#1050).

## polars 0.16.1

This is a small hot-fix release to update dependent Rust polars to 0.39.1 (#1042).
Expand Down
66 changes: 40 additions & 26 deletions R/expr__expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,41 +565,55 @@ Expr_alias = use_extendr_wrapper
#' Apply logical AND on a column
#'
#' Check if all values in a Boolean column are `TRUE`. This method is an
#' expression - not to be confused with `pl$all()` which is a function to select
#' all columns.
#' @param drop_nulls Logical. Default TRUE, as name says.
#' @return Boolean literal
#' expression - not to be confused with [`pl$all()`][pl_all] which is a function
#' to select all columns.
#'
#' @param ignore_nulls If `TRUE` (default), ignore null values. If `FALSE`,
#' [Kleene logic](https://en.wikipedia.org/wiki/Three-valued_logic) is used to
#' deal with nulls: if the column contains any null values and no `TRUE` values,
#' the output is null.
#'
#' @return A logical value
#' @examples
#' pl$DataFrame(
#' all = c(TRUE, TRUE),
#' any = c(TRUE, FALSE),
#' none = c(FALSE, FALSE)
#' )$select(
#' # the first $all() selects all columns, the second one applies the AND
#' # logical on the values
#' pl$all()$all()
#' df = pl$DataFrame(
#' a = c(TRUE, TRUE),
#' b = c(TRUE, FALSE),
#' c = c(NA, TRUE),
#' d = c(NA, NA)
#' )
Expr_all = function(drop_nulls = TRUE) {
.pr$Expr$all(self, drop_nulls) |>
unwrap("in $all()")
#'
#' # By default, ignore null values. If there are only nulls, then all() returns
#' # TRUE.
#' df$select(pl$col("*")$all())
#'
#' # If we set ignore_nulls = FALSE, then we don't know if all values in column
#' # "c" are TRUE, so it returns null
#' df$select(pl$col("*")$all(ignore_nulls = FALSE))
Expr_all = function(ignore_nulls = TRUE) {
etiennebacher marked this conversation as resolved.
Show resolved Hide resolved
.pr$Expr$all(self, ignore_nulls) |>
unwrap("in $all():")
}

#' Apply logical OR on a column
#'
#' Check if any boolean value in a Boolean column is `TRUE`.
#' @param drop_nulls Logical. Default TRUE, as name says.
#' @return Boolean literal
#'
#' @inherit Expr_all params return
#' @examples
#' pl$DataFrame(
#' all = c(TRUE, TRUE),
#' any = c(TRUE, FALSE),
#' none = c(FALSE, FALSE)
#' )$select(
#' pl$all()$any()
#' df = pl$DataFrame(
#' a = c(TRUE, FALSE),
#' b = c(FALSE, FALSE),
#' c = c(NA, FALSE)
#' )
Expr_any = function(drop_nulls = TRUE) {
.pr$Expr$any(self, drop_nulls) |>
unwrap("in $all()")
#'
#' df$select(pl$col("*")$any())
#'
#' # If we set ignore_nulls = FALSE, then we don't know if any values in column
#' # "c" is TRUE, so it returns null
#' df$select(pl$col("*")$any(ignore_nulls = FALSE))
Expr_any = function(ignore_nulls = TRUE) {
.pr$Expr$any(self, ignore_nulls) |>
unwrap("in $any():")
}

#' Count elements
Expand Down
4 changes: 2 additions & 2 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,9 @@ RPolarsExpr$unique_stable <- function() .Call(wrap__RPolarsExpr__unique_stable,

RPolarsExpr$agg_groups <- function() .Call(wrap__RPolarsExpr__agg_groups, self)

RPolarsExpr$all <- function(drop_nulls) .Call(wrap__RPolarsExpr__all, self, drop_nulls)
RPolarsExpr$all <- function(ignore_nulls) .Call(wrap__RPolarsExpr__all, self, ignore_nulls)

RPolarsExpr$any <- function(drop_nulls) .Call(wrap__RPolarsExpr__any, self, drop_nulls)
RPolarsExpr$any <- function(ignore_nulls) .Call(wrap__RPolarsExpr__any, self, ignore_nulls)

RPolarsExpr$is_between <- function(lower, upper, closed) .Call(wrap__RPolarsExpr__is_between, self, lower, upper, closed)

Expand Down
2 changes: 0 additions & 2 deletions man/ExprArr_max.Rd

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

2 changes: 0 additions & 2 deletions man/ExprArr_min.Rd

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

34 changes: 21 additions & 13 deletions man/Expr_all.Rd

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

25 changes: 16 additions & 9 deletions man/Expr_any.Rd

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

8 changes: 4 additions & 4 deletions src/rust/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,11 +1658,11 @@ impl RPolarsExpr {

// boolean

pub fn all(&self, drop_nulls: Robj) -> RResult<Self> {
Ok(self.0.clone().all(robj_to!(bool, drop_nulls)?).into())
pub fn all(&self, ignore_nulls: Robj) -> RResult<Self> {
Ok(self.0.clone().all(robj_to!(bool, ignore_nulls)?).into())
}
pub fn any(&self, drop_nulls: Robj) -> RResult<Self> {
Ok(self.0.clone().any(robj_to!(bool, drop_nulls)?).into())
pub fn any(&self, ignore_nulls: Robj) -> RResult<Self> {
Ok(self.0.clone().any(robj_to!(bool, ignore_nulls)?).into())
}

fn is_between(&self, lower: Robj, upper: Robj, closed: Robj) -> RResult<Self> {
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-expr_expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -2702,3 +2702,41 @@ test_that("rle_id works", {
)
)
})

test_that("any works", {
df = pl$DataFrame(
a = c(TRUE, FALSE),
b = c(FALSE, FALSE),
c = c(NA, FALSE),
d = c(NA, NA)
)

expect_identical(
df$select(pl$col("*")$any())$to_list(),
list(a = TRUE, b = FALSE, c = FALSE, d = FALSE)
)

expect_identical(
df$select(pl$col("*")$any(ignore_nulls = FALSE))$to_list(),
list(a = TRUE, b = FALSE, c = NA, d = NA)
)
})

test_that("all works", {
df = pl$DataFrame(
a = c(TRUE, TRUE),
b = c(TRUE, FALSE),
c = c(NA, TRUE),
d = c(NA, NA)
)

expect_identical(
df$select(pl$col("*")$all())$to_list(),
list(a = TRUE, b = FALSE, c = TRUE, d = TRUE)
)

expect_identical(
df$select(pl$col("*")$all(ignore_nulls = FALSE))$to_list(),
list(a = TRUE, b = FALSE, c = NA, d = NA)
)
})
Loading