Skip to content

Commit

Permalink
better negation of duration string
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed May 21, 2024
1 parent 9a089bf commit d27cd07
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,7 @@ DataFrame_rolling = function(
group_by = NULL,
check_sorted = TRUE) {
period = parse_as_polars_duration_string(period)
offset = parse_as_polars_duration_string(offset) %||% paste0("-", period)
offset = parse_as_polars_duration_string(offset) %||% negate_duration_string(period)
construct_rolling_group_by(self, index_column, period, offset, closed, group_by, check_sorted)
}

Expand Down Expand Up @@ -2216,7 +2216,7 @@ DataFrame_group_by_dynamic = function(
start_by = "window",
check_sorted = TRUE) {
every = parse_as_polars_duration_string(every)
offset = parse_as_polars_duration_string(offset) %||% paste0("-", every)
offset = parse_as_polars_duration_string(offset) %||% negate_duration_string(every)
period = parse_as_polars_duration_string(period) %||% every
construct_group_by_dynamic(
self, index_column, every, period, offset, include_boundaries, closed, label,
Expand Down
2 changes: 1 addition & 1 deletion R/expr__expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -3321,7 +3321,7 @@ Expr_rolling = function(
closed = "right",
check_sorted = TRUE) {
period = parse_as_polars_duration_string(period)
offset = parse_as_polars_duration_string(offset) %||% paste0("-", period)
offset = parse_as_polars_duration_string(offset) %||% negate_duration_string(period)
.pr$Expr$rolling(self, index_column, period, offset, closed, check_sorted) |>
unwrap("in $rolling():")
}
Expand Down
4 changes: 2 additions & 2 deletions R/lazyframe__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,7 @@ LazyFrame_rolling = function(
group_by = NULL,
check_sorted = TRUE) {
period = parse_as_polars_duration_string(period)
offset = parse_as_polars_duration_string(offset) %||% paste0("-", period)
offset = parse_as_polars_duration_string(offset) %||% negate_duration_string(period)
.pr$LazyFrame$rolling(
self, index_column, period, offset, closed,
wrap_elist_result(group_by, str_to_lit = FALSE), check_sorted
Expand Down Expand Up @@ -2025,7 +2025,7 @@ LazyFrame_group_by_dynamic = function(
start_by = "window",
check_sorted = TRUE) {
every = parse_as_polars_duration_string(every)
offset = parse_as_polars_duration_string(offset) %||% paste0("-", every)
offset = parse_as_polars_duration_string(offset) %||% negate_duration_string(every)
period = parse_as_polars_duration_string(period) %||% every

.pr$LazyFrame$group_by_dynamic(
Expand Down
8 changes: 8 additions & 0 deletions R/parse_as_duration.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ difftime_to_duration_string = function(dft) {
)
paste0(value, unit)
}

negate_duration_string = function(x) {
if (startsWith(x, "-")) {
gsub("^-", "", x)
} else {
paste0("-", x)
}
}
12 changes: 12 additions & 0 deletions tests/testthat/test-groupby.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ test_that("group_by_dynamic for LazyFrame: error if not explicitly sorted", {
)
})

test_that("group_by_dynamic for LazyFrame: error if every is negative", {
df = pl$LazyFrame(
idx = 0:5,
n = 0:5
)$with_columns(pl$col("idx")$set_sorted())

expect_grepl_error(
df$group_by_dynamic("idx", every = "-2i")$agg(pl$col("n")$mean())$collect(),
"'every' argument must be positive"
)
})

test_that("group_by_dynamic for LazyFrame: arg 'closed' works", {
df = pl$LazyFrame(
dt = c(
Expand Down

0 comments on commit d27cd07

Please sign in to comment.