From 8adf4eb548ef6e612b8440e9844e096f061fd22c Mon Sep 17 00:00:00 2001 From: LunaticSage218 Date: Mon, 20 Apr 2026 12:55:27 -0700 Subject: [PATCH] Added format specifier for issue #7694 --- NEWS.md | 2 ++ R/IDateTime.R | 10 +++++++++- inst/tests/tests.Rraw | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 4e64f3faa0..9e6d959dc5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -48,6 +48,8 @@ 8. `frollapply()` no longer produces output longer than the input when the window length is also longer than the input [#7646](https://github.com/Rdatatable/data.table/issues/7646). Thanks to @hadley-johnson for reporting and @jangorecki for the fix. +9. `yearqtr()` now accepts an optional format specifier [#7694](https://github.com/Rdatatable/data.table/issues/7694). 'numeric' is the deafult, which preserves the original behavior, but 'character' formats the date like so: YYYYQ# (e.g. 2025Q2). Thanks to @jan-swissre for the report and @LunaticSage218 for the implementation. + ### Notes 1. {data.table} now depends on R 3.5.0 (2018). diff --git a/R/IDateTime.R b/R/IDateTime.R index 49fa5abda2..772934e70d 100644 --- a/R/IDateTime.R +++ b/R/IDateTime.R @@ -366,7 +366,15 @@ month = function(x) convertDate(as.IDate(x), "month") quarter = function(x) convertDate(as.IDate(x), "quarter") year = function(x) convertDate(as.IDate(x), "year") yearmon = function(x) convertDate(as.IDate(x), "yearmon") -yearqtr = function(x) convertDate(as.IDate(x), "yearqtr") +yearqtr = function(x, format=c("numeric", "character")) { + format = match.arg(format) + if (format == "numeric") return(convertDate(as.IDate(x), "yearqtr")) + yr = convertDate(as.IDate(x), "year") + qtr = convertDate(as.IDate(x), "quarter") + ans = paste0(yr, "Q", qtr) + ans[is.na(as.IDate(x))] = NA_character_ + ans +} convertDate = function(x, type) { type = match.arg(type, c("yday", "wday", "mday", "week", "month", "quarter", "year", "yearmon", "yearqtr")) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index b73b2767a8..9924b5f5cd 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21577,3 +21577,12 @@ close(con) file.create(f <- tempfile()) test(2367.6, fread(file(f)), data.table(), warning="Connection has size 0.") unlink(f) + +# yearqtr() could optionally output 2025Q4 format #7694 +x = c("1111-11-11", "2019-01-01", "2019-02-28", "2019-03-01", "2019-12-31", "2020-02-29", "2020-03-01", "2020-12-31", "2040-01-01", "2040-12-31", "2100-03-01", NA) +test(2368.1, yearqtr(x, format="numeric"), c(1111.75, 2019, 2019, 2019, 2019.75, 2020, 2020, 2020.75, 2040, 2040.75, 2100, NA)) +test(2368.2, yearqtr(x, format="numeric"), yearqtr(x)) # numeric is the default, preserves backwards compatibility +test(2368.3, yearqtr(x, format="character"), c("1111Q4", "2019Q1", "2019Q1", "2019Q1", "2019Q4", "2020Q1", "2020Q1", "2020Q4", "2040Q1", "2040Q4", "2100Q1", NA_character_)) +test(2368.4, yearqtr("2016-08-03 01:02:03.45", format="character"), "2016Q3") +test(2368.5, yearqtr(NA, format="character"), NA_character_) +test(2368.6, yearqtr(x, format="invalid"), error="should be one of")