Skip to content

Commit 84a3279

Browse files
authored
Merge branch 'master' into ReformattingAndCleanup
2 parents d99bcbc + 7f14f2e commit 84a3279

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ if (getRversion() >= "3.6.0") {
153153

154154
# IDateTime support:
155155
export(as.IDate,as.ITime,IDateTime)
156-
export(second,minute,hour,yday,wday,mday,week,isoweek,month,quarter,year,yearmon,yearqtr)
156+
export(second,minute,hour,yday,wday,mday,week,isoweek,isoyear,month,quarter,year,yearmon,yearqtr)
157157

158158
S3method("[", ITime)
159159
S3method("+", IDate)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767

6868
14. `fcoalesce()` and `setcoalesce()` gain `nan` argument to control whether `NaN` values should be treated as missing (`nan=NA`, the default) or non-missing (`nan=NaN`), [#4567](https://github.com/Rdatatable/data.table/issues/4567). This provides full compatibility with `nafill()` behavior. Thanks to @ethanbsmith for the feature request and @Mukulyadav2004 for the implementation.
6969

70+
15. New function `isoyear()` has been implemented as a complement to `isoweek()`, returning the ISO 8601 year corresponding to a given date, [#7154](https://github.com/Rdatatable/data.table/issues/7154). Thanks to @ben-schwen and @MichaelChirico for the suggestion and @venom1204 for the implementation.
71+
7072
### BUG FIXES
7173

7274
1. `fread()` no longer warns on certain systems on R 4.5.0+ where the file owner can't be resolved, [#6918](https://github.com/Rdatatable/data.table/issues/6918). Thanks @ProfFancyPants for the report and PR.

R/IDateTime.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ isoweek = function(x) as.integer(format(as.IDate(x), "%V"))
355355
# nearest_thurs = as.IDate(7L * (as.integer(x + 3L) %/% 7L))
356356
# year_start = as.IDate(format(nearest_thurs, '%Y-01-01'))
357357
# 1L + (nearest_thurs - year_start) %/% 7L
358-
358+
isoyear = function(x) as.integer(format(as.IDate(x), "%G"))
359359

360360
month = function(x) convertDate(as.IDate(x), "month")
361361
quarter = function(x) convertDate(as.IDate(x), "quarter")

inst/tests/nafill.Rraw

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ test(3.02, setnafill(list(copy(x)), "locf", fill=0L), list(x))
114114
test(3.03, setnafill(x, "locf"), error="in-place update is supported only for list")
115115
test(3.04, nafill(letters[1:5], fill=0), error="must be numeric type, or list/data.table")
116116
test(3.05, setnafill(list(letters[1:5]), fill=0), error="must be numeric type, or list/data.table")
117-
test(3.06, nafill(x, fill=1:2), error="fill must be a vector of length 1")
118-
test(3.07, nafill(x, fill="asd"), x, warning=c("Coercing.*character.*integer","NAs introduced by coercion"))
117+
test(3.06, nafill(x, fill=1:2), error="fill must be a vector of length 1.*fcoalesce")
118+
test(3.07, nafill(x, "locf", fill=1:2), error="fill must be a vector of length 1.*x\\.$")
119+
test(3.08, nafill(x, fill="asd"), x, warning=c("Coercing.*character.*integer","NAs introduced by coercion"))
119120

120121
# colnamesInt helper
121122
dt = data.table(a=1, b=2, d=3)

inst/tests/tests.Rraw

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21549,3 +21549,11 @@ f = tempfile()
2154921549
writeLines(c('a', rep('0x1.ffffp0', 10000L), '0x1.ff\x9fp0', rep('0x1.ffffp0', 20000L)), f)
2155021550
test(2334, names(fread(f)), "a")
2155121551
unlink(f)
21552+
21553+
# Tests for new isoyear() helper (complement to isoweek) #7154
21554+
test(2335.1, isoyear(as.IDate("2019-12-30")), 2020L) # End of year edge case
21555+
test(2335.2, isoyear(as.IDate("2016-01-01")), 2015L) # Start of year edge case
21556+
test(2335.3, isoyear(as.IDate("2023-08-15")), 2023L) # Normal mid-year case
21557+
test(2335.4, isoyear(as.IDate(c("2019-12-30", "2016-01-01", "2023-08-15"))),c(2020L, 2015L, 2023L))
21558+
test(2335.5, isoyear("2019-12-30"), 2020L)
21559+
test(2335.6, isoyear(as.Date("2019-12-30")), 2020L)

man/IDateTime.Rd

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
\alias{mday}
3939
\alias{week}
4040
\alias{isoweek}
41+
\alias{isoyear}
4142
\alias{month}
4243
\alias{quarter}
4344
\alias{year}
@@ -92,6 +93,7 @@ wday(x)
9293
mday(x)
9394
week(x)
9495
isoweek(x)
96+
isoyear(x)
9597
month(x)
9698
quarter(x)
9799
year(x)
@@ -187,6 +189,8 @@ which specify that the first week of the year is the one containing the first Th
187189
This convention ensures that week boundaries align consistently with year boundaries,
188190
accounting for both year transitions and varying day counts per week.
189191
192+
Similarly, \code{isoyear()} returns the ISO 8601 year corresponding to the ISO week.
193+
190194
}
191195
192196
\value{
@@ -200,7 +204,7 @@ accounting for both year transitions and varying day counts per week.
200204
\code{itime} in \code{IDate} and \code{ITime} format.
201205
202206
\code{second}, \code{minute}, \code{hour}, \code{yday}, \code{wday},
203-
\code{mday}, \code{week}, \code{month}, \code{quarter},
207+
\code{mday}, \code{week}, \code{isoweek}, \code{isoyear}, \code{month}, \code{quarter},
204208
and \code{year} return integer values
205209
for second, minute, hour, day of year, day of week,
206210
day of month, week, month, quarter, and year, respectively.
@@ -281,6 +285,17 @@ round(seqdates, "months")
281285
round(seqtimes, "hours")
282286
trunc(seqtimes, "hours")
283287

288+
# Examples for isoyear() and isoweek()
289+
d1 = as.IDate("2019-12-30")
290+
year(d1)
291+
isoweek(d1)
292+
isoyear(d1)
293+
294+
d2 = as.IDate("2016-01-01")
295+
year(d2)
296+
isoweek(d2)
297+
isoyear(d2)
298+
284299
}
285300
\keyword{utilities}
286301

src/nafill.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,14 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
186186
isInt64[i] = INHERITS(VECTOR_ELT(x, i), char_integer64);
187187
const void **fillp = (const void**)R_alloc(nx, sizeof(*fillp)); // fill is (or will be) a list of length nx of matching types, scalar values for each column, this pointer points to each of those columns data pointers
188188
if (hasFill) {
189-
if (nx != length(fill) && length(fill) != 1)
190-
error(_("fill must be a vector of length 1 or a list of length of x"));
189+
190+
if (nx != length(fill) && length(fill) != 1) {
191+
if (itype == 0) {
192+
error(_("fill must be a vector of length 1 or a list of length of x. Consider fcoalesce() to specify element-wise replacements."));
193+
} else {
194+
error(_("fill must be a vector of length 1 or a list of length of x."));
195+
}
196+
}
191197
if (!isNewList(fill)) {
192198
SEXP fill1 = fill;
193199
fill = PROTECT(allocVector(VECSXP, nx)); protecti++;

0 commit comments

Comments
 (0)