Skip to content

Commit 14d60a5

Browse files
feat: Implement <expr>$dt$days_in_month() (#1659)
1 parent 768ba0a commit 14d60a5

File tree

9 files changed

+63
-0
lines changed

9 files changed

+63
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `<expr>$item()` to strictly extract a single value from an expression (#1652).
88
- `<expr>$arr$eval()` to run any Polars expression on all subarrays of an Array column (#1653).
99
- `<expr>$name$replace()` to replace expression names using regular expressions (#1654).
10+
- `<expr>$dt$days_in_month()` (#1659).
1011

1112
## polars 1.6.0
1213

R/000-wrappers.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,12 @@ class(`PlRDataTypeExpr`) <- c("PlRDataTypeExpr__bundle", "savvy_polars__sealed")
16491649
}
16501650
}
16511651

1652+
`PlRExpr_dt_days_in_month` <- function(self) {
1653+
function() {
1654+
.savvy_wrap_PlRExpr(.Call(savvy_PlRExpr_dt_days_in_month__impl, `self`))
1655+
}
1656+
}
1657+
16521658
`PlRExpr_dt_dst_offset` <- function(self) {
16531659
function() {
16541660
.savvy_wrap_PlRExpr(.Call(savvy_PlRExpr_dt_dst_offset__impl, `self`))
@@ -3529,6 +3535,7 @@ class(`PlRDataTypeExpr`) <- c("PlRDataTypeExpr__bundle", "savvy_polars__sealed")
35293535
e$`dt_convert_time_zone` <- `PlRExpr_dt_convert_time_zone`(ptr)
35303536
e$`dt_date` <- `PlRExpr_dt_date`(ptr)
35313537
e$`dt_day` <- `PlRExpr_dt_day`(ptr)
3538+
e$`dt_days_in_month` <- `PlRExpr_dt_days_in_month`(ptr)
35323539
e$`dt_dst_offset` <- `PlRExpr_dt_dst_offset`(ptr)
35333540
e$`dt_epoch_seconds` <- `PlRExpr_dt_epoch_seconds`(ptr)
35343541
e$`dt_hour` <- `PlRExpr_dt_hour`(ptr)

R/expr-datetime.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,3 +1240,17 @@ expr_dt_replace <- function(
12401240
)
12411241
})
12421242
}
1243+
1244+
#' Extract the number of days in the month from the underlying Date representation.
1245+
#'
1246+
#' @inherit as_polars_expr return
1247+
#' @examples
1248+
#' df <- pl$DataFrame(date = as.Date(c("2020-01-01", "2020-02-01", "2020-03-01")))
1249+
#'
1250+
#' df$with_columns(
1251+
#' days_in_month = pl$col("date")$dt$days_in_month()
1252+
#' )
1253+
expr_dt_days_in_month <- function() {
1254+
self$`_rexpr`$dt_days_in_month() |>
1255+
wrap()
1256+
}

altdoc/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ nav:
633633
- dt_convert_time_zone: man/expr_dt_convert_time_zone.md
634634
- dt_date: man/expr_dt_date.md
635635
- dt_day: man/expr_dt_day.md
636+
- dt_days_in_month: man/expr_dt_days_in_month.md
636637
- dt_dst_offset: man/expr_dt_dst_offset.md
637638
- dt_epoch: man/expr_dt_epoch.md
638639
- dt_hour: man/expr_dt_hour.md

man/expr_dt_days_in_month.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/init.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,11 @@ SEXP savvy_PlRExpr_dt_day__impl(SEXP self__) {
11991199
return handle_result(res);
12001200
}
12011201

1202+
SEXP savvy_PlRExpr_dt_days_in_month__impl(SEXP self__) {
1203+
SEXP res = savvy_PlRExpr_dt_days_in_month__ffi(self__);
1204+
return handle_result(res);
1205+
}
1206+
12021207
SEXP savvy_PlRExpr_dt_dst_offset__impl(SEXP self__) {
12031208
SEXP res = savvy_PlRExpr_dt_dst_offset__ffi(self__);
12041209
return handle_result(res);
@@ -3634,6 +3639,7 @@ static const R_CallMethodDef CallEntries[] = {
36343639
{"savvy_PlRExpr_dt_convert_time_zone__impl", (DL_FUNC) &savvy_PlRExpr_dt_convert_time_zone__impl, 2},
36353640
{"savvy_PlRExpr_dt_date__impl", (DL_FUNC) &savvy_PlRExpr_dt_date__impl, 1},
36363641
{"savvy_PlRExpr_dt_day__impl", (DL_FUNC) &savvy_PlRExpr_dt_day__impl, 1},
3642+
{"savvy_PlRExpr_dt_days_in_month__impl", (DL_FUNC) &savvy_PlRExpr_dt_days_in_month__impl, 1},
36373643
{"savvy_PlRExpr_dt_dst_offset__impl", (DL_FUNC) &savvy_PlRExpr_dt_dst_offset__impl, 1},
36383644
{"savvy_PlRExpr_dt_epoch_seconds__impl", (DL_FUNC) &savvy_PlRExpr_dt_epoch_seconds__impl, 1},
36393645
{"savvy_PlRExpr_dt_hour__impl", (DL_FUNC) &savvy_PlRExpr_dt_hour__impl, 1},

src/rust/api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ SEXP savvy_PlRExpr_dt_combine__ffi(SEXP self__, SEXP c_arg__time, SEXP c_arg__ti
245245
SEXP savvy_PlRExpr_dt_convert_time_zone__ffi(SEXP self__, SEXP c_arg__time_zone);
246246
SEXP savvy_PlRExpr_dt_date__ffi(SEXP self__);
247247
SEXP savvy_PlRExpr_dt_day__ffi(SEXP self__);
248+
SEXP savvy_PlRExpr_dt_days_in_month__ffi(SEXP self__);
248249
SEXP savvy_PlRExpr_dt_dst_offset__ffi(SEXP self__);
249250
SEXP savvy_PlRExpr_dt_epoch_seconds__ffi(SEXP self__);
250251
SEXP savvy_PlRExpr_dt_hour__ffi(SEXP self__);

src/rust/src/expr/datetime.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,8 @@ impl PlRExpr {
253253
)
254254
.into())
255255
}
256+
257+
fn dt_days_in_month(&self) -> Result<Self> {
258+
Ok(self.inner.clone().dt().days_in_month().into())
259+
}
256260
}

tests/testthat/test-expr-datetime.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,3 +1012,11 @@ patrick::with_parameters_test_that(
10121012
}
10131013
}
10141014
)
1015+
1016+
test_that("dt$days_in_month", {
1017+
df <- pl$DataFrame(date = as.Date(c("2020-01-01", "2020-02-01", "2020-03-01")))
1018+
expect_equal(
1019+
df$select(pl$col("date")$dt$days_in_month()),
1020+
pl$DataFrame(date = c(31, 29, 31))$cast(pl$Int8)
1021+
)
1022+
})

0 commit comments

Comments
 (0)