首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >data.table fread和ISO8601

data.table fread和ISO8601
EN

Stack Overflow用户
提问于 2022-02-23 17:29:05
回答 1查看 188关注 0票数 4

我觉得有点傻,因为我看不出问题.

新闻文件指出,fread可以正确地识别ISO8601时间戳,如2020-07-24T10:11:12.134Z (从v1.13.0开始)。但这是不正确的:

代码语言:javascript
复制
fread(text=c("now","2020-07-24T10:11:12.134Z"), colClasses="POSIXct", sep=",")
#           now
#        <POSc>
# 1: 2020-07-24

但是,如果我将T更改为空格,它将返回正确的时间戳:

代码语言:javascript
复制
fread(text=c("now","2020-07-24 10:11:12.134Z"), colClasses="POSIXct", sep=",")
#                    now
#                 <POSc>
# 1: 2020-07-24 10:11:12

如果我使用tz=""tz="UTC",仍然会发生这种情况。(毫不奇怪,如果我省略了colClasses=,它甚至不会尝试转换。)

要让fread的内部和更快的POSIXct转换器工作,我做错了什么?如果需要的话,我知道如何做这个后读,但我有一个大得多的文件,其中使用as.POSIXct后读是一种惩罚。

(Windows-11,R-4.1.2,数据表-1.14.2)

如果感兴趣的话,verbose=TRUE似乎没有提供太多的见解:

代码语言:javascript
复制
fread(text=c("now","2020-07-24T10:11:12.134Z"), colClasses="POSIXct", sep=",", verbose=TRUE)
#   OpenMP version (_OPENMP)       201511
#   omp_get_num_procs()            16
#   R_DATATABLE_NUM_PROCS_PERCENT  unset (default 50)
#   R_DATATABLE_NUM_THREADS        unset
#   R_DATATABLE_THROTTLE           unset (default 1024)
#   omp_get_thread_limit()         2147483647
#   omp_get_max_threads()          16
#   OMP_THREAD_LIMIT               unset
#   OMP_NUM_THREADS                unset
#   RestoreAfterFork               true
#   data.table is using 8 threads with throttle==1024. See ?setDTthreads.
# Input contains no \n. Taking this to be a filename to open
# [01] Check arguments
#   Using 8 threads (omp_get_max_threads()=16, nth=8)
#   NAstrings = [<<NA>>]
#   None of the NAstrings look like numbers.
#   show progress = 1
#   0/1 column will be read as integer
# [02] Opening the file
#   Opening file C:\Users\r2\AppData\Local\Temp\Rtmpao7n9S\file49384a01388a
#   File opened, size = 31 bytes.
#   Memory mapped ok
# [03] Detect and skip BOM
# [04] Arrange mmap to be \0 terminated
#   \n has been found in the input and different lines can end with different line endings (e.g. mixed \n and \r\n in one file). This is common and ideal.
# [05] Skipping initial rows if needed
#   Positioned on line 1 starting: <<now>>
# [06] Detect separator, quoting rule, and ncolumns
#   Using supplied sep ','
#   No sep and quote rule found a block of 2x2 or greater. Single column input.
#   Detected 1 columns on line 1. This line is either column names or first data row. Line starts as: <<now>>
#   Quote rule picked = 0
#   fill=false and the most number of columns found is 1
# [07] Detect column types, good nrow estimate and whether first row is column names
#   Number of sampling jump points = 1 because (29 bytes from row 1 to eof) / (2 * 29 jump0size) == 0
#   Type codes (jump 000)    : C  Quote rule 0
#   'header' determined to be true because all columns are type string and a better guess is not possible
#   All rows were sampled since file is small so we know nrow=1 exactly
# [08] Assign column names
# [09] Apply user overrides on column types
#   After 0 type and 0 drop user overrides : C
# [10] Allocate memory for the datatable
#   Allocating 1 column slots (1 - 0 dropped) with 1 rows
# [11] Read the data
#   jumps=[0..1), chunk_size=1048576, total_size=24
# Read 1 rows x 1 columns from 31 bytes file in 00:00.000 wall clock time
# [12] Finalizing the datatable
#   Type counts:
#          1 : string    'C'
# =============================
#    0.000s (  0%) Memory map 0.000GB file
#    0.000s (  0%) sep='' ncol=1 and header detection
#    0.000s (  0%) Column type detection using 1 sample rows
#    0.000s (  0%) Allocation of 1 rows x 1 cols (0.000GB) of which 1 (100%) rows used
#    0.000s (  0%) Reading 1 chunks (0 swept) of 1.000MB (each chunk 1 rows) using 1 threads
#    +    0.000s (  0%) Parse to row-major thread buffers (grown 0 times)
#    +    0.000s (  0%) Transpose
#    +    0.000s (  0%) Waiting
#    0.000s (  0%) Rereading 0 columns due to out-of-sample type exceptions
#    0.000s        Total
#           now
#        <POSc>
# 1: 2020-07-24

fread(text=c("now","2020-07-24 10:11:12.134Z"), colClasses="POSIXct", sep=",", verbose=TRUE)
#   OpenMP version (_OPENMP)       201511
#   omp_get_num_procs()            16
#   R_DATATABLE_NUM_PROCS_PERCENT  unset (default 50)
#   R_DATATABLE_NUM_THREADS        unset
#   R_DATATABLE_THROTTLE           unset (default 1024)
#   omp_get_thread_limit()         2147483647
#   omp_get_max_threads()          16
#   OMP_THREAD_LIMIT               unset
#   OMP_NUM_THREADS                unset
#   RestoreAfterFork               true
#   data.table is using 8 threads with throttle==1024. See ?setDTthreads.
# Input contains no \n. Taking this to be a filename to open
# [01] Check arguments
#   Using 8 threads (omp_get_max_threads()=16, nth=8)
#   NAstrings = [<<NA>>]
#   None of the NAstrings look like numbers.
#   show progress = 1
#   0/1 column will be read as integer
# [02] Opening the file
#   Opening file C:\Users\r2\AppData\Local\Temp\Rtmpao7n9S\file493817cf4117
#   File opened, size = 31 bytes.
#   Memory mapped ok
# [03] Detect and skip BOM
# [04] Arrange mmap to be \0 terminated
#   \n has been found in the input and different lines can end with different line endings (e.g. mixed \n and \r\n in one file). This is common and ideal.
# [05] Skipping initial rows if needed
#   Positioned on line 1 starting: <<now>>
# [06] Detect separator, quoting rule, and ncolumns
#   Using supplied sep ','
#   No sep and quote rule found a block of 2x2 or greater. Single column input.
#   Detected 1 columns on line 1. This line is either column names or first data row. Line starts as: <<now>>
#   Quote rule picked = 0
#   fill=false and the most number of columns found is 1
# [07] Detect column types, good nrow estimate and whether first row is column names
#   Number of sampling jump points = 1 because (29 bytes from row 1 to eof) / (2 * 29 jump0size) == 0
#   Type codes (jump 000)    : C  Quote rule 0
#   'header' determined to be true because all columns are type string and a better guess is not possible
#   All rows were sampled since file is small so we know nrow=1 exactly
# [08] Assign column names
# [09] Apply user overrides on column types
#   After 0 type and 0 drop user overrides : C
# [10] Allocate memory for the datatable
#   Allocating 1 column slots (1 - 0 dropped) with 1 rows
# [11] Read the data
#   jumps=[0..1), chunk_size=1048576, total_size=24
# Read 1 rows x 1 columns from 31 bytes file in 00:00.000 wall clock time
# [12] Finalizing the datatable
#   Type counts:
#          1 : string    'C'
# =============================
#    0.000s (  0%) Memory map 0.000GB file
#    0.000s (  0%) sep='' ncol=1 and header detection
#    0.000s (  0%) Column type detection using 1 sample rows
#    0.000s (  0%) Allocation of 1 rows x 1 cols (0.000GB) of which 1 (100%) rows used
#    0.000s (  0%) Reading 1 chunks (0 swept) of 1.000MB (each chunk 1 rows) using 1 threads
#    +    0.000s (  0%) Parse to row-major thread buffers (grown 0 times)
#    +    0.000s (  0%) Transpose
#    +    0.000s (  0%) Waiting
#    0.000s (  0%) Rereading 0 columns due to out-of-sample type exceptions
#    0.000s        Total
#                        now
#                     <POSc>
# 1: 2020-07-24 10:11:12.134

当使用file=而不是text=时,此行为不会改变。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-23 17:58:40

不确定这是否出于设计,但罪魁祸首是keepLeadingZeros=TRUE,这是我出于其他原因设置的一个选项。

代码语言:javascript
复制
withr::with_options(
  list(datatable.keepLeadingZeros=FALSE), 
  fread(text=c("now","2020-07-24T10:11:12.134Z"), sep=",")
)
#                        now
#                     <POSc>
# 1: 2020-07-24 10:11:12.134

withr::with_options(
  list(datatable.keepLeadingZeros=TRUE), 
  fread(text=c("now","2020-07-24T10:11:12.134Z"), sep=",")
)
#                         now
#                      <char>
# 1: 2020-07-24T10:11:12.134Z

事后,我在https://github.com/Rdatatable/data.table/issues/4869上发现了"keepLeadingZeros干扰日期识别“这一问题。

FYI (对其他人和我未来的自我),我发现它的方式是启动R --vanilla --no-init --no-save,安装deta.table,并开始测试:

代码语言:javascript
复制
### in "failing" environment:
opts <- options()
opts <- opts[ !sapply(opts, inherits, c("list", "function")) ]
dput(opts) # paste into the fresh R instance as opts2

### in the "fresh "environment"
# opts2 <- structure(...) # 'opts' from above
opts <- options()
opts <- opts[ !sapply(opts, inherits, c("list", "function")) ]
str(opts[ setdiff(names(opts2), names(opts)) ])

以及一个接一个的启用选项,直到自动转换失败。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71241559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档