Skip to content

Commit

Permalink
fix: $len() should also count null values (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Apr 16, 2024
1 parent 2d8d0e5 commit bb234ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## polars (development version)

### Bug fixes

* `$len()` now correctly includes `null` values in the count (#1044).

## Polars R Package 0.16.0

### Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ impl RPolarsExpr {
}

pub fn len(&self) -> Self {
self.0.clone().count().into()
self.0.clone().len().into()
}

pub fn slice(&self, offset: &RPolarsExpr, length: Nullable<&RPolarsExpr>) -> Self {
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-expr_expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ test_that("count + unique + n_unique", {
)
})

test_that("$len() and $count() don't have the same behavior for nulls", {
expect_equal(
pl$DataFrame(x = c(1, 2, NA))$select(pl$col("x")$len())$to_list(),
list(x = 3)
)
expect_equal(
pl$DataFrame(x = c(1, 2, NA))$select(pl$col("x")$count())$to_list(),
list(x = 2)
)
})


test_that("drop_nans drop_nulls", {
x = c(1.0, 2.0, NaN, NA)
Expand Down

0 comments on commit bb234ed

Please sign in to comment.