Data.table: Completing the roster of .IDate methods with companion .Date methods

Created on 9 May 2016  路  4Comments  路  Source: Rdatatable/data.table

Comparing

methods(class = "IDate")
#  [1] as.Date     as.list     as.POSIXct  c           coerce      cut        
#  [7] initialize  mean        rep         round       seq         show       
# [13] slotsFromS3 split       unique  

With

methods(class = "Date")
#  [1] as.character  as.data.frame as.data.table as.IDate      as.list      
#  [6] as.POSIXct    as.POSIXlt    Axis          c             coerce       
# [11] cut           -             [<-           [             [[           
# [16] +             diff          format        hist          initialize   
# [21] is.numeric    julian        Math          mean          months       
# [26] Ops           pretty        print         quarters      rep          
# [31] round         seq           show          slotsFromS3   split        
# [36] str           summary       Summary       trunc         weekdays     
# [41] weighted.mean xtfrm

We see there's a lot of minor stuff to take care of to get IDate on par with Date for built in methods (and to help make sure we're not accidentally converting to Date along the way).

Specifically, we can add the following:

  • [ ] as.character
  • [ ] as.data.frame
  • [ ] as.data.table
  • [ ] as.POSIXlt
  • [ ] Axis
  • [ ] -
  • [x] [<-
  • [ ] [
  • [ ] [[
  • [ ] diff
  • [ ] format
  • [ ] hist
  • [ ] is.numeric
  • [ ] julian
  • [ ] Math
  • [ ] mean
  • [ ] months
  • [ ] Ops
  • [ ] pretty
  • [ ] print
  • [ ] quarters
  • [ ] split
  • [ ] str
  • [ ] summary
  • [ ] Summary
  • [x] trunc
  • [ ] weekdays
  • [ ] weighted.mean
  • [x] xtfrm
idatitime

Most helpful comment

Was about to post a new issue (titled "replace() returns invalid IDates") before I saw this. There are good reasons for at least some of these besides efficiency, I guess.

Here's an example for [<-.IDate:

library(data.table)
z = replace(as.IDate(Sys.Date() + 1:2), 1L, as.IDate(Sys.Date()-1L))
dput(z)
# structure(c(17189, 17192), class = c("IDate", "Date"))

As you can see, the storage format of the vector is not integer and yet the class still claims to be IDate.

This was a problem for me because I stored this vector as a column alongside a legit IDate and then used melt, which correctly gave me the warning

'measure.vars' [z, d] are not all of the same type. [...]

and gave me back an unclassed double.

Maybe the problem is that replace uses [<-, and that gets dispatched to [<-.Date which contains a pernicious as.Date(value) line, so maybe a dedicated [<-.IDate.

Btw, thanks for taking these on, Michael.

All 4 comments

but they should be dispatched to next method, so Date method, we may only need to adjust class on returning object.
Anyway I would prefer to wait for #1451

The hope is that there's some efficiency gains, especially if the Date method forces numeric before we force integer again.

I'm gonna handle this myself, so no need to divert resources from more important things.

Was about to post a new issue (titled "replace() returns invalid IDates") before I saw this. There are good reasons for at least some of these besides efficiency, I guess.

Here's an example for [<-.IDate:

library(data.table)
z = replace(as.IDate(Sys.Date() + 1:2), 1L, as.IDate(Sys.Date()-1L))
dput(z)
# structure(c(17189, 17192), class = c("IDate", "Date"))

As you can see, the storage format of the vector is not integer and yet the class still claims to be IDate.

This was a problem for me because I stored this vector as a column alongside a legit IDate and then used melt, which correctly gave me the warning

'measure.vars' [z, d] are not all of the same type. [...]

and gave me back an unclassed double.

Maybe the problem is that replace uses [<-, and that gets dispatched to [<-.Date which contains a pernicious as.Date(value) line, so maybe a dedicated [<-.IDate.

Btw, thanks for taking these on, Michael.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sengoku93 picture sengoku93  路  3Comments

jameslamb picture jameslamb  路  3Comments

st-pasha picture st-pasha  路  3Comments

mattdowle picture mattdowle  路  3Comments

jangorecki picture jangorecki  路  3Comments