Repository navigation
Rbind in 1.14.3 doesn't like POSIX #5309
Copy link
Copy link
Milestone
Description
Looks like rbind (and by extension merge(all = T)) doesn't like POSIXct type data in 1.14.3. To the extent this is a legit bug, I think its been introduced relatively recently (code running on an older version of 1.14.3 seemed to work just fine).
Sys.setenv(TZ = 'America/Los_Angeles')
library('data.table')
#> Warning: package 'data.table' was built under R version 4.1.2
t2 = data.table(a = NA_real_, b = NA_real_, c = NA_real_, d= as.Date(NA))
t1 = data.table(a = 1.1, b = 1.1, c = 1.1, d = as.Date('2021-10-05'), e = as.POSIXct("2021-10-06 13:58:00 UTC"))
r2 = rbind(t1,t2, fill = T, use.names = F)
#> Error in rbindlist(l, use.names, fill, idcol): Class attribute on column 5 of item 2 does not match with column 5 of item 1.
sessionInfo()
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows Server x64 (build 19041)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=English_United States.1252
#> [2] LC_CTYPE=English_United States.1252
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C
#> [5] LC_TIME=English_United States.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] data.table_1.14.3
#>
#> loaded via a namespace (and not attached):
#> [1] ps_1.6.0 digest_0.6.27 withr_2.4.2 magrittr_2.0.1
#> [5] reprex_2.0.1 evaluate_0.14 highr_0.9 stringi_1.6.2
#> [9] rlang_0.4.11 cli_2.5.0 rstudioapi_0.13 fs_1.5.0
#> [13] rmarkdown_2.11 tools_4.1.0 stringr_1.4.0 glue_1.4.2
#> [17] xfun_0.29 yaml_2.2.1 fastmap_1.1.0 compiler_4.1.0
#> [21] htmltools_0.5.2 knitr_1.33Under 1.14.2:
Sys.setenv(TZ = 'America/Los_Angeles')
library('data.table')
#> Warning: package 'data.table' was built under R version 4.1.2
t2 = data.table(a = NA_real_, b = NA_real_, c = NA_real_, d= as.Date(NA))
t1 = data.table(a = 1.1, b = 1.1, c = 1.1, d = as.Date('2021-10-05'), e = as.POSIXct("2021-10-06 13:58:00 UTC"))
r2 = rbind(t1,t2, fill = T, use.names = F)
#> Warning in rbindlist(l, use.names, fill, idcol): use.names= cannot be FALSE when
#> fill is TRUE. Setting use.names=TRUE.
sessionInfo()
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows Server x64 (build 19041)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=English_United States.1252
#> [2] LC_CTYPE=English_United States.1252
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C
#> [5] LC_TIME=English_United States.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] data.table_1.14.2
#>
#> loaded via a namespace (and not attached):
#> [1] ps_1.6.0 digest_0.6.27 withr_2.4.2 magrittr_2.0.1
#> [5] reprex_2.0.1 evaluate_0.14 highr_0.9 stringi_1.6.2
#> [9] rlang_0.4.11 cli_2.5.0 rstudioapi_0.13 fs_1.5.0
#> [13] rmarkdown_2.11 tools_4.1.0 stringr_1.4.0 glue_1.4.2
#> [17] xfun_0.29 yaml_2.2.1 fastmap_1.1.0 compiler_4.1.0
#> [21] htmltools_0.5.2 knitr_1.33Reactions are currently unavailable
Activity
dcaseykc commentedon Jan 12, 2022
Some git bisecting suggests commit: 4922384 is where this got introduced.
fox34 commentedon Jan 28, 2022
Combining
NAs andPOSIXctdoes not work with Version 1.14.2 either. So this is related to non-existing columns defaulting toNAwithfill=TRUE(in your example: columneonly exists int1):I stumbled across this issue, too, and my workaround was to temporarily convert
POSIXcttodoubleand then convert back as needed withas.POSIXct(..., origin="1970-01-01", tz="...").adrian-quintario commentedon Aug 18, 2022
Can confirm this happens with both
base::Dateandzoo::Datetoo:berg-michael commentedon Aug 22, 2022
I've run into this issue with IDates. I note that while rbind fails on both versions 1.14.2 and 1.14.3 with the toy data below, the merge is actually successful on 1.14.2 but not 1.14.3. I haven't exhaustively looked into this, but reverting the change made to rbindlist.c in #5263 at least makes the merge work. I think this is probably the same issue as #5391 .
1.14.3 (neither merge nor rbind work with IDate)
Created on 2022-08-22 by the reprex package (v2.0.1)
R 1.14.2 (merge works, rbind doesn't)
Created on 2022-08-22 by the reprex package (v2.0.1)
R 1.14.3 but with change to rbindlist.c reverted
Created on 2022-08-22 by the reprex package (v2.0.1)
jangorecki commentedon Aug 23, 2022
Thank you for extra report. We need ensure that goes into unit tests to close this issue.
berg-michael commentedon Aug 23, 2022
BTW, in the current dev release, the order of x and y in the merge command can determine whether this issue is triggered.
Created on 2022-08-23 by the reprex package (v2.0.1)
ben-schwen commentedon Aug 24, 2022
@berg-michael IMO your first example of (shortened it)
should never work because of different types. Ofc we could check if all values of a column are
NAand bump types but the cost and loss of understanding seem not worth it.berg-michael commentedon Aug 24, 2022
This may be an ignorant question, but doesn't your example work for non-date classes? I can run something like
It seems like rbind will coerce classes/types in most but not all situations, and merge with all = T can rely on that behavior when there are non-matching rows between two datasets. Though I don't fully understand why the call you gave doesn't work in either 1.14.2 and 1.14.3, yet merge works fine in these cases in 1.14.2 while sometimes breaking in 1.14.3. I believe it is related to allowing usenames = F when fill = T, as if I force usenames to T in 1.14.3 the merge works fine.
I guess what you describe is really just #3911.
ben-schwen commentedon Aug 24, 2022
I see, so apparently, we do type bumping for atomic types. If we allow different
classeswe have to take care of things like this where e.g. first class (or class with highest types) determines class of resultadd fix #5309
add tests from #5309
remove use of rbindlist(..., use.names=FALSE, fill=TRUE) in merge (#5857
Rbind allow binding of different class attributes (#5446)