Rdatatable / data.table

R's data.table package extends data.frame:
http://r-datatable.com
Mozilla Public License 2.0
3.62k stars 986 forks source link

Subsetting with Date produces error #4651

Open renkun-ken opened 4 years ago

renkun-ken commented 4 years ago

It is known that using a numeric vector in i will be treated as subsetting by index. However, if the supplied numeric vector has some classes (e.g. Date), then it might make more sense not to subset by index but join (just like a character vector).

Consider the following example:

library(data.table)
test <- data.table(
  date = seq.Date(as.Date("2020-01-01"), as.Date("2020-07-01"), by = "day"),
  key = "date"
)
test[, x := rnorm(.N)]
test[as.Date("2020-01-02")]

The following error is produced:

> test[as.Date("2020-01-02")]                                                                   
Error in `[.data.table`(test, as.Date("2020-01-02")) : 
  i has evaluated to type double. Expecting logical, integer or double.
Backtrace:
1: stop("i has evaluated to type ", typeof(i), ". Expecting logical, integer or double.")
2: `[.data.table`(test, as.Date("2020-01-02"))
3: test[as.Date("2020-01-02")]

User might expect it to behave like

> test[.(as.Date("2020-01-02"))]                                                                
Key: <date>
         date          x
       <Date>      <num>
1: 2020-01-02 -0.2380267
MichaelChirico commented 4 years ago

Hmm I think I would prefer to just implement #3506

ColeMiller1 commented 4 years ago

This is slightly different. This is suggesting using NSE to transform a bare date into a list date, similar to what happens with bare characters.

https://github.com/Rdatatable/data.table/blob/db618444a4d16b91cccf35d80fa3f306739dd571/R/data.table.R#L397-L405

It would be easy to accommodate if this is something that should be supported. It would be good to have #4585 merged first as there would be some conflicts otherwise although I realize the other PR might be too much change.