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

Change $drop() input from columns to ... #914

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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 R Package (development version)

### New features

- `$drop()` now accepts several character vectors, such as `$drop("a", "b")`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suspect this is a potentially breaking change.
In other words, if you specify it with the columns argument instead of the position argument, won't unexpected behavior occur?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch. Actually I think there's a bug in dots_to_colnames() because passing c() in second values doesn't work:

### this works
pl$DataFrame(mtcars)$drop(c("mpg", "drat"), "hp") |> ncol()
[1] 8

### this doesn't
pl$DataFrame(mtcars)$drop("hp", c("mpg", "drat")) |> ncol()

Error: Execution halted with the following contexts
   0: In R: in $drop():
   0: During function call [ncol(pl$DataFrame(mtcars)$drop("hp", c("mpg", "drat")))]
   1: When constructing a Column Expr
   2: The argument [ `...` ] caused an error
   3: Possibly because element no. [1] 
   4: Expected a value of type [alloc::string::String]
   5: Got value [Rvalue: ["mpg", "drat"], Rsexp: Strings, Rclass: ["character"]]

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a problem of pl$col(), not dots_to_colnames().

> library(polars)

> pl$col(c("mpg", "drat"), "hp")
polars Expr: cols(["mpg", "drat", "hp"])

> pl$col("hp", c("mpg", "drat"))
Error: Execution halted with the following contexts
   0: In R: in pl$col()
   0: During function call [pl$col("hp", c("mpg", "drat"))]
   1: When constructing a Column Expr
   2: The argument [ `...` ] caused an error
   3: Possibly because element no. [1] 
   4: Expected a value of type [alloc::string::String]
   5: Got value [Rvalue: ["mpg", "drat"], Rsexp: Strings, Rclass: ["character"]]

> pl$col("hp", name = c("mpg", "drat"))
polars Expr: cols(["mpg", "drat", "hp"])

> pl$col("hp", columns = c("mpg", "drat"))
Error: Execution halted with the following contexts
   0: In R: in pl$col()
   0: During function call [pl$col("hp", columns = c("mpg", "drat"))]
   1: When constructing a Column Expr
   2: The argument [ `...` ] caused an error
   3: Possibly because element no. [1] 
   4: Expected a value of type [alloc::string::String]
   5: Got value [Rvalue: ["mpg", "drat"], Rsexp: Strings, Rclass: ["character"]]

We should rewrite pl$col()...(#912)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Something like pl$DataFrame(mtcars)$drop(columns = c("mpg", "drat")) works in the main branch, but should fail in this PR because of pl$col(name = "", ...).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, I've updated news, thanks

(#912).

## Polars R Package 0.15.1

### New features
Expand Down
17 changes: 11 additions & 6 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,18 @@ DataFrame.property_setters$columns = function(self, names) {
}


#' @title Drop columns of a DataFrame
#' @keywords DataFrame
#' @param columns A character vector with the names of the column(s) to remove.
#' Drop columns of a DataFrame
#'
#' @param ... Characters of column names to drop. Passed to [`pl$col()`][pl_col].
#'
#' @return DataFrame
#' @examples pl$DataFrame(mtcars)$drop(c("mpg", "hp"))
DataFrame_drop = function(columns) {
self$lazy()$drop(columns)$collect()
#' @examples
#' pl$DataFrame(mtcars)$drop(c("mpg", "hp"))
#'
#' # equivalent
#' pl$DataFrame(mtcars)$drop("mpg", "hp")
DataFrame_drop = function(...) {
self$lazy()$drop(...)$collect()
}


Expand Down
19 changes: 14 additions & 5 deletions R/lazyframe__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -1022,13 +1022,22 @@ 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():")
}

#' @title Drop columns of a LazyFrame
#' @keywords LazyFrame
#' Drop columns of a LazyFrame
#'
#' @inheritParams DataFrame_drop
#'
#' @return LazyFrame
#' @examples pl$LazyFrame(mtcars)$drop(c("mpg", "hp"))
LazyFrame_drop = function(columns) {
unwrap(.pr$LazyFrame$drop(self, columns), "in $drop():")
#' @examples
#' pl$LazyFrame(mtcars)$drop(c("mpg", "hp"))$collect()
#'
#' # equivalent
#' pl$LazyFrame(mtcars)$drop("mpg", "hp")$collect()
LazyFrame_drop = function(...) {
uw = \(res) unwrap(res, "in $drop():")
cols = result(dots_to_colnames(self, ...)) |>
uw()
.pr$LazyFrame$drop(self, cols) |>
uw()
}

#' @title Reverse
Expand Down
8 changes: 5 additions & 3 deletions man/DataFrame_drop.Rd

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

10 changes: 6 additions & 4 deletions man/LazyFrame_drop.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,10 @@ test_that("drop", {
a = pl$DataFrame(mtcars)$drop(c("mpg", "hp"))$columns
expect_false("hp" %in% a)
expect_false("mpg" %in% a)
a = pl$DataFrame(mtcars)$drop(c("mpg", "drat"), "hp")$columns
expect_false("hp" %in% a)
expect_false("mpg" %in% a)
expect_false("drat" %in% a)
a = pl$DataFrame(mtcars)$drop("mpg")$columns
expect_true("hp" %in% a)
expect_false("mpg" %in% a)
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ test_that("drop", {
a = pl$DataFrame(mtcars)$lazy()$drop(c("mpg", "hp"))$collect()$columns
expect_false("hp" %in% a)
expect_false("mpg" %in% a)
a = pl$DataFrame(mtcars)$lazy()$drop(c("mpg", "drat"), "hp")$collect()$columns
expect_false("hp" %in% a)
expect_false("mpg" %in% a)
expect_false("drat" %in% a)
a = pl$DataFrame(mtcars)$lazy()$drop("mpg")$collect()$columns
expect_true("hp" %in% a)
expect_false("mpg" %in% a)
Expand Down Expand Up @@ -534,7 +538,7 @@ test_that("join_asof_simple", {

# test if setting was as expected in LogicalPlan
expect_identical(get_reg(logical_json_plan_TT, allow_p_pat), "\"allow_parallel\": Bool(true)")
expect_identical(get_reg(logical_json_plan_TT,force_p_pat),"\"force_parallel\": Bool(true)")
expect_identical(get_reg(logical_json_plan_TT, force_p_pat), "\"force_parallel\": Bool(true)")
expect_identical(get_reg(logical_json_plan_FF, allow_p_pat), "\"allow_parallel\": Bool(false)")
expect_identical(get_reg(logical_json_plan_FF, force_p_pat), "\"force_parallel\": Bool(false)")
})
Expand Down
Loading