Skip to content

Commit 0a11ffa

Browse files
Add shift() error for Date/POSIXct mismatch (#7174)
* add NEWS * add fix * add tests * add tests * tweak --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent ae5b4a5 commit 0a11ffa

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
8585
13. Reference to `.SD` in `...` arguments to `lapply()`, e.g. ``lapply(list_of_tables, `[`, j=.SD[1L])`` is evaluated correctly, [#2982](https://github.com/Rdatatable/data.table/issues/2982). Thanks @franknarf1 for the report and @MichaelChirico for the fix.
8686
87+
14. Filling columns of class Date with POSIXct (and vice versa) using `shift()` now yields a clear, informative error message specifying the class mismatch, [#5218](https://github.com/Rdatatable/data.table/issues/5218). Thanks @ashbaldry for the report and @ben-schwen for the fix.
88+
8789
### NOTES
8890
8991
1. The following in-progress deprecations have proceeded:

inst/tests/tests.Rraw

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6851,6 +6851,13 @@ test(1463.78, shift(x,1:2, type="cyclic"), list(as.raw(c(5, 1:4)), as.raw(c(4:
68516851
test(1463.79, shift(x,-1L, type="cyclic"), as.raw(c(2:5, 1)))
68526852
test(1463.80, shift(x,-(1:2),type="cyclic"), list(as.raw(c(2:5, 1)), as.raw(c(3:5,1:2))))
68536853

6854+
# shift incompatible types (e.g. Date and POSIXct)
6855+
d = .Date(0:4)
6856+
p = .POSIXct(1:5)
6857+
test(1463.81, shift(d, fill=p[1L]), error="Filling Date with POSIXct .* unsupported.*")
6858+
test(1463.82, shift(p, fill=d[1L]), error="Filling POSIXct with Date .* unsupported.*")
6859+
test(1463.83, shift(d, fill=as.IDate(2000L)), .Date(c(2000L, 0:3)))
6860+
68546861
# FR #686
68556862
DT = data.table(a=rep(c("A", "B", "C", "A", "B"), c(2,2,3,1,2)), foo=1:10)
68566863
# Seemingly superfluous 'foo' is needed to test fix for #1942

src/shift.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ SEXP shift(SEXP obj, SEXP k, SEXP fill, SEXP type)
4040
SEXP elem = VECTOR_ELT(x, i);
4141
size_t size = RTYPE_SIZEOF(elem);
4242
R_xlen_t xrows = xlength(elem);
43+
if (INHERITS(elem, char_Date) && INHERITS(fill, char_POSIXct) || INHERITS(elem, char_POSIXct) && INHERITS(fill, char_Date)) {
44+
const char* elem_type = INHERITS(elem, char_Date) ? "Date" : "POSIXct";
45+
const char* fill_type = INHERITS(fill, char_Date) ? "Date" : "POSIXct";
46+
error(_("Filling %s with %s using shift() is unsupported. Please convert fill to %s first."),
47+
elem_type, fill_type, elem_type);
48+
}
4349
SEXP thisfill = PROTECT(coerceAs(fill, elem, ScalarLogical(0))); // #4865 use coerceAs for type coercion
4450
switch (TYPEOF(elem)) {
4551
case INTSXP: case LGLSXP: {

0 commit comments

Comments
 (0)