Skip to content

fread/fwrite *base data types* directly for efficiency #1656

Description

@jangorecki

There could be an option for fwrite and fread to dump/read the unclassed representation of objects. In such a case whole IO process can be much faster, because we don't have to format POSIXct, [I]Date and other classes for writing to file, we just unclass them, and write numbers (or any other base type they use) to a file. Later when reading with fread we provide colClasses (or yaml), and those numbers (or another base types) are properly recognized as their original class.

library(data.table)
dt = data.table(a=as.Date("2015-01-01"), b=as.POSIXct("2015-01-01 15:30:20"))
fwrite(dt, "fwrite-tests.csv", AsIs=TRUE)
system("cat fwrite-tests.csv")
#a, b
#16436, 1420106420
fread("fwrite-tests.csv", colClasses = c(a="Date", b="POSIXct"))
#            a                   b
#       <Date>              <POSc>
#1: 2015-01-01 2015-01-01 15:30:20

Activity

  1. changed the title [-]fread could read *base data types* directly for efficiency[/-] [+]fread/fwrite *base data types* directly for efficiency[/+] on Apr 20, 2016
  2. clarkdk commented on May 10, 2016

    @clarkdk

    It would be awesome if fwrite by default wrote Date and POSIXct objects in standard formats (ie ISO 8601, or Lubridate) that fread would automatically recognize and read in as Date or POSIXct, without requiring colClasses to be specified for each datetime column. A default automatic approach would be really welcome since I spend a lot of programming time converting datetime character strings back to POSIXct when importing time series data, even when I have written the file myself with write.csv. It would presumably also require an assumption (default value) for tz on import, unless the default format included the time zone offset on every value. Perhaps a POSIXct.format="format-string" parameter could be included to specify the format string to use when writing and reading POSIXct values, since colClasses = c(a="Date", b="POSIXct") doesn't allow a specific format and/or timezone to be specified.

  3. jangorecki commented on May 10, 2016

    @jangorecki
    MemberAuthor

    @clarkdk You discuss quite a different issue, as it requires parsing strings that stores date/datetime. This issue is about date/datetime stored as numerics, and those can be effectively optimized for IO speed if fwrite and fread would be able to write and read those types as their numerics transparently (or using just colClasses). So no parsing is required. If you would like to speedup parsing strings then fasttime can help.

  4. clarkdk commented on May 10, 2016

    @clarkdk

    @jangorecki OK, now I understand. I will make a separate FR. In implementing datetimes as numerics in fread/fwrite, please don't rule out a possible future feature involving writing and parsing of datetimes as formatted strings.

  5. added this to the milestone on Nov 11, 2016
  6. modified the milestones: , on Mar 17, 2018
  7. modified the milestones: v1.11.0, on Apr 29, 2018
  8. modified the milestones: 1.12.0, 1.12.2 on Jan 11, 2019
  9. removed this from the 1.12.2 milestone on Jan 24, 2019
  10. 2 remaining items

  11. modified the milestones: 1.12.4, 1.13.0 on Jul 25, 2019
  12. PavoDive commented on Oct 25, 2019

    @PavoDive

    Not sure if this belongs to this issue or to 3391 or to the master task 2247, but I could reproduce an unexpected behavior with fread and fwrite when yaml = TRUE:

    dt = data.table(date = as.POSIXct(c("2006-05-01", "2006-05-02")), 
                     b = as.factor(c(1,2)), 
                     c = c(3,4))
    print(class(dt$date))
    fwrite(dt, "dt.csv", yaml = TRUE)
    
    dt2 = fread("dt.csv", yaml = TRUE)
    print(class(dt2$date))
    

    See: https://stackoverflow.com/questions/58493926/fread-with-yaml-true-read-type-date-as-character

  13. MichaelChirico commented on Oct 25, 2019

    @MichaelChirico
    Member

    @PavoDive could you file a new issue for that? please & thank you

  14. modified the milestones: 1.12.7, 1.12.9 on Dec 8, 2019
  15. modified the milestones: 1.13.1, 1.13.3 on Oct 17, 2020
  16. modified the milestones: 1.14.3, on Jul 19, 2022
  17. modified the milestones: , 1.15.1 on Oct 29, 2023
  18. removed this from the 1.16.0 milestone on Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions