【问题标题】:How to parse milliseconds?如何解析毫秒?
【发布时间】:2011-01-10 03:28:32
【问题描述】:

如何使用strptime 或任何其他函数来解析 R 中以毫秒为单位的时间戳?

time[1]
# [1] "2010-01-15 13:55:23.975"
strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
# [1] NA
strptime(time[1], format="%Y-%m-%d %H:%M:%S")
# [1] "2010-01-15 13:55:23"`

【问题讨论】:

    标签: r datetime time-series strptime


    【解决方案1】:

    感谢?strptime 帮助文件(示例已更改为您的值):

     z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS")
     z # prints without fractional seconds
     op <- options(digits.secs=3)
     z
     options(op) #reset options
    

    【讨论】:

    • 谢谢,我在 strptime 文档中错过了这一点。一直在找格式字符,没看到就放弃了。
    • 如果我可以为你设立一个纪念碑,我会的!
    • 我也是! “%OS”位太棒了。
    • 这只是在python3还是什么?在我的 python2.7.8 中: >>> time.strptime(t, "%Y-%m-%d %H:%M:%OS") Traceback (last last 最近调用): File "", line 1、在文件“/opt/pythons/2.7.8/lib/python2.7/_strptime.py”,第467行,在_strptime_time中返回_strptime(data_string, format)[0]文件“/opt/pythons/ 2.7.8/lib/python2.7/_strptime.py",第 317 行,在 _strptime (bad_directive, format)) ValueError: 'O' is a bad directive in format '%Y-%m-%d %H:% M:%OS'
    • @firebush:它在 R 中。在某些平台上可能需要 "%Y-%m-%d %H:%M:%OS3"。 “操作系统”格式的实现在帮助页面上标记为特定于操作系统。
    【解决方案2】:

    您也可以使用strptime(time[1], "%OSn") where 0 digits.secs。

    文档指出“支持哪些取决于操作系统。”所以YMMV。

    【讨论】:

    • 这在 Ubuntu 上对我也不起作用。这会产生 NA 。
    猜你喜欢
    • 2015-09-16
    • 1970-01-01
    • 2015-10-17
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多