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

Implement $struct$with_fields() #1109

Merged
merged 56 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
199b263
refactor: simplify
eitsupi May 9, 2024
960b401
init [skip ci]
etiennebacher May 21, 2024
092bb4c
wip [skip ci]
eitsupi May 22, 2024
5ad880b
Merge branch 'main' into rust-polars-0.40.0 [skip ci]
eitsupi May 22, 2024
3de9b81
fix some methods lazyframe [skip ci]
etiennebacher May 22, 2024
5b3fa5f
more fixes for LazyFrame [skip ci]
etiennebacher May 22, 2024
5057f8a
Merge branch 'main' into rust-polars-0.40.0
eitsupi May 22, 2024
808861c
chore: simplify the manifest file [skip ci]
eitsupi May 22, 2024
20751a8
fix warnings [skip ci]
eitsupi May 22, 2024
de9eead
fix mean for Series [skip ci]
eitsupi May 22, 2024
f21fc03
fix join [skip ci]
etiennebacher May 22, 2024
6d3814d
fix some agg funcs for Series [skip ci]
eitsupi May 23, 2024
6c4f500
fix result [skip ci]
eitsupi May 23, 2024
e15226d
fix function for Series [skip ci]
eitsupi May 23, 2024
1eb4245
remove unused argument [skip ci]
eitsupi May 23, 2024
5f68255
fix: update Unknown type
eitsupi May 23, 2024
743e3d0
chore: update wrapper file
eitsupi May 23, 2024
55f2621
chore: auto update file
eitsupi May 23, 2024
3a46431
refactor: remove unused arguments
eitsupi May 23, 2024
57ecc9e
fix: fix some functions no longer return result
eitsupi May 23, 2024
548c2d9
chore: update Rd files by the latest dev packages
eitsupi May 23, 2024
6a2cac8
fix: new argument
eitsupi May 23, 2024
72fa7f8
fix: fix the polas vignette
eitsupi May 23, 2024
9af9085
fix top_k
etiennebacher May 23, 2024
34196eb
fix test for CSV read
etiennebacher May 23, 2024
07279d4
fix test for is_not_nan() [skip ci]
etiennebacher May 23, 2024
6cfe501
new snapshot for $meta$tree_format() [skip ci]
etiennebacher May 23, 2024
ff446de
fix bottom_k() [skip ci]
etiennebacher May 23, 2024
1d212f8
fix lazyframe print [skip ci]
etiennebacher May 23, 2024
a5269eb
remove memory_map arg from read_/scan_ipc() [skip ci]
etiennebacher May 24, 2024
a3a0e0a
remove now useless arguments in rolling_() exprs
etiennebacher May 24, 2024
b662e92
init rolling_*_by() on the rust side [skip ci]
etiennebacher May 24, 2024
6891054
snapshot
etiennebacher May 25, 2024
7e84104
top_k and bottom_k should use maintain_order arg [skip ci]
etiennebacher May 25, 2024
e172366
comment out test for $meta$pop() [skip ci]
etiennebacher May 25, 2024
bb992c2
Revert "remove memory_map arg from read_/scan_ipc() [skip ci]"
eitsupi May 26, 2024
0113a9d
Merge branch 'fix-rechunk' into rust-polars-0.40.0
eitsupi May 26, 2024
09d6012
chore: regen Rd files
eitsupi May 26, 2024
e9ed4d7
test: remove meaningless test
eitsupi May 26, 2024
4f157bf
test: name functions seems broken
eitsupi May 26, 2024
49e6ff3
test: update snapshot
eitsupi May 26, 2024
0b0a443
fix: remove unused argument from `from_raw_ipc`
eitsupi May 26, 2024
6c1364b
docs: remove unused arguments
eitsupi May 26, 2024
5d3c76c
test: disable panicked test
eitsupi May 26, 2024
dabbcaf
Revert "test: name functions seems broken"
eitsupi May 26, 2024
88608f3
test: skip expr_name tests
eitsupi May 26, 2024
0186054
feat: use JEMALLOC on all unix architectures
eitsupi May 26, 2024
1a22ec3
do not skip all tests for $name subnamespace
etiennebacher May 26, 2024
b0406e5
conditionally run examples for $name$map() [skip ci]
etiennebacher May 26, 2024
01e9a66
news
etiennebacher May 26, 2024
f723337
init
etiennebacher May 26, 2024
513f423
merge main
etiennebacher May 26, 2024
c4c2b40
test
etiennebacher May 26, 2024
33369b2
news
etiennebacher May 26, 2024
cc729c4
snapshot
etiennebacher May 26, 2024
4abc227
fix
etiennebacher May 26, 2024
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
like `pl$Struct(a = pl$Boolean)` (#1053).
- In `$all()` and `$any()`, the argument `drop_nulls` is renamed `ignore_nulls`,
and this argument must be named (#1050).
- New method `$struct$with_fields()` (#1109).

## Polars R Package 0.16.4

Expand Down
37 changes: 35 additions & 2 deletions R/expr__struct.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#' pl$col("struct_col")$struct$field("ddd")
#' )
ExprStruct_field = function(name) {
.pr$Expr$struct_field_by_name(self, result(name)) |> unwrap("in struct$field:")
.pr$Expr$struct_field_by_name(self, name) |>
unwrap("in $struct$field():")
}


Expand All @@ -47,5 +48,37 @@ ExprStruct_field = function(name) {
#' )
#' df$unnest()
ExprStruct_rename_fields = function(names) {
.pr$Expr$struct_rename_fields(self, result(names)) |> unwrap("in struct$rename_fields:")
.pr$Expr$struct_rename_fields(self, names) |> unwrap("in $struct$rename_fields:")
}

#' Add or overwrite fields of this struct
#'
#' This is similar to [`with_columns`][DataFrame_with_columns] on
#' [`DataFrame`][RPolarsDataFrame].
#'
#' @param ... Field(s) to add. Accepts expression input. Strings are parsed as
#' column names, other non-expression inputs are parsed as literals.
#'
#' @return An [`Expr`][RPolarsExpr] of data type Struct.
#'
#' @examples
#' df = pl$DataFrame(x = c(1, 4, 9), y = c(4, 9, 16), multiply = c(10, 2, 3))$
#' with_columns(coords = pl$struct(c("x", "y")))$
#' select("coords", "multiply")
#'
#' df
#'
#' df = df$with_columns(
#' pl$col("coords")$struct$with_fields(
#' pl$col("coords")$struct$field("x")$sqrt(),
#' y_mul = pl$col("coords")$struct$field("y") * pl$col("multiply")
#' )
#' )
#'
#' df
#'
#' df$unnest("coords")
ExprStruct_with_fields = function(...) {
.pr$Expr$struct_with_fields(self, unpack_list(..., .context = "in $struct$with_fields()")) |>
unwrap("in $struct$with_fields:")
}
2 changes: 2 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,8 @@ RPolarsExpr$struct_field_by_name <- function(name) .Call(wrap__RPolarsExpr__stru

RPolarsExpr$struct_rename_fields <- function(names) .Call(wrap__RPolarsExpr__struct_rename_fields, self, names)

RPolarsExpr$struct_with_fields <- function(fields) .Call(wrap__RPolarsExpr__struct_with_fields, self, fields)

RPolarsExpr$meta_pop <- function() .Call(wrap__RPolarsExpr__meta_pop, self)

RPolarsExpr$meta_eq <- function(other) .Call(wrap__RPolarsExpr__meta_eq, self, other)
Expand Down
37 changes: 37 additions & 0 deletions man/ExprStruct_with_fields.Rd

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

5 changes: 5 additions & 0 deletions src/rust/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,11 @@ impl RPolarsExpr {
Ok(self.0.clone().struct_().rename_fields(string_vec).into())
}

fn struct_with_fields(&self, fields: Robj) -> RResult<Self> {
let fields = robj_to!(VecPLExprColNamed, fields)?;
Ok(self.0.clone().struct_().with_fields(fields).into())
}

//placed in py-polars/src/lazy/meta.rs, however extendr do not support
//multiple export impl.
fn meta_pop(&self) -> List {
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/_snaps/after-wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,14 @@
[311] "str_to_lowercase" "str_to_time"
[313] "str_to_titlecase" "str_to_uppercase"
[315] "str_zfill" "struct_field_by_name"
[317] "struct_rename_fields" "sub"
[319] "sum" "tail"
[321] "tan" "tanh"
[323] "to_physical" "top_k"
[325] "unique" "unique_counts"
[327] "unique_stable" "upper_bound"
[329] "value_counts" "var"
[331] "xor"
[317] "struct_rename_fields" "struct_with_fields"
[319] "sub" "sum"
[321] "tail" "tan"
[323] "tanh" "to_physical"
[325] "top_k" "unique"
[327] "unique_counts" "unique_stable"
[329] "upper_bound" "value_counts"
[331] "var" "xor"

# public and private methods of each class When

Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-expr_struct.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ test_that("expr struct$rename_fields", {
err_state = result(pl$col("")$struct$rename_fields(42))
expect_grepl_error(unwrap(err_state), "str")
expect_grepl_error(unwrap(err_state), "\\[names\\]")
expect_grepl_error(unwrap(err_state), "in struct\\$rename_fields:")
expect_grepl_error(unwrap(err_state), "in \\$struct\\$rename_fields:")
expect_grepl_error(unwrap(err_state), "42.0")
})

test_that("$struct$with_fields", {
df = pl$DataFrame(x = c(1, 4, 9), y = c(4, 9, 16), multiply = c(10, 2, 3))$
with_columns(coords = pl$struct(c("x", "y")))$
select("coords", "multiply")

out = df$select(
pl$col("coords")$struct$with_fields(
pl$col("coords")$struct$field("x")$sqrt(),
y_mul = pl$col("coords")$struct$field("y") * pl$col("multiply")
)
)$unnest("coords")

expect_identical(
out$to_data_frame(),
data.frame(x = c(1, 2, 3), y = c(4, 9, 16), y_mul = c(40, 18, 48))
)
})
Loading