如何从numpy.datetime64获取unix时间戳

时间:2012-08-08 13:19:15

标签: python numpy

如何从numpy.datetime64或numpy.datetime _获取UNIX时间?

例如:

np.datetime_('2012-08-08 13:37:00')

6 个答案:

答案 0 :(得分:6)

numpy datetime64有可变单位:

摘自official doc

  

内部存储单元自动从字符串的形式中选择,可以是日期单位或时间单位。日期单位是年('Y'),月('M'),周('W')和天('D'),而时间单位是小时('h'),分钟('m') ),秒('s'),毫秒('ms'),以及一些额外的SI前缀秒数单位。

因此,首先我们需要使用dtype检查当前单位,例如:

>>> now = np.datetime64(datetime.datetime.now())
>>> now.dtype

# for ns unit, use:
dtype('<M8[ns]')
now.astype('int64')/1e9, dtype='int32'

# for us unit, use:
dtype('<M8[us]')
now.astype('int64')/1e6, dtype='int32'

# for ms unit, use:
dtype('<M8[ms]')
now.astype('int64')/1e3, dtype='int32'

依旧......

答案 1 :(得分:4)

我在numpy 1.6.1与1.7上的np.datetime64('now')值得到的结果不一致。

这适用于:

>>> import datetime
>>> import numpy as np
>>> now = np.datetime64(datetime.datetime.now())
>>> (now.astype('uint64') / 1e6).astype('uint32')
1344447810

答案 2 :(得分:3)

In order to account for the units, I think you need to do something like:

d2.A.values[:] = 2
d2

   A  B
0  2  a

Note that this converts to 'seconds' (the @GET @Path("/{filename : .*}") public Response getFile(@PathParam("filename") String filename) { String f = getFileFromSomewhere(filename); StreamingOutput fileStream = new StreamingOutput() { @Override public void write(java.io.OutputStream output) throws IOException, WebApplicationException { try { java.nio.file.Path path = Paths.get(f); byte[] data = Files.readAllBytes(path); output.write(data); output.flush(); } catch (Exception e) { throw new WebApplicationException("File Not Found !!"); } } }; return Response .ok(fileStream) .build(); } ) prior to converting to integers. This works on NumPy 1.12.1.

答案 3 :(得分:0)

首先,您必须知道阵列的存储单元。然后,您将数组视为64位整数,然后除以适当的缩放因子以返回秒。例如,如果您的日期时间数组以微秒(--- title: "It works!" author: "trianglegirl" date: "May 20, 2019" output: html_document --- ```{js jQuery-codechunk} const trianglegirl_function = function() { // Test that jQuery works in Rmd $('h1').css('color', 'red'); // Updating classes does not work $("tr:contains('Mazda')").addClass('Mazda'); // Colour styling also does not work $("tr:contains('Mazda')").css("color", "red"); }; ``` ```{r R-codechunk} library(DT) DT::datatable(mtcars, callback = JS('trianglegirl_function();')) ``` )的存储单位存储,则可以这样做:

dtype=<M8[us]

答案 4 :(得分:0)

我想发布我发现的解决方案,因为我认为类型转换可能存在问题,所以我认为这可能比转换为uint更好。

SELECT OBITNO as "product"
    , OBORNO as "Order #"
    , summery FROM MVXJDTA.OOLINE md1 
    LEFT JOIN ( 
            SELECT OBITNO as "product"
            , sum(OBORQA) as "summery" 
              FROM MVXJDTA.OOLINE WHERE OBCONO=2 and OBWHLO=091 and 
              OBCODT>20190701 group by OBITNO) md2 
    on md2.OBITNO=md1.OBITNO
WHERE md2.OBCONO=2 and md2.OBWHLO=091 and md2.OBCODT>20190701

我觉得此解决方案要好一些,因为它允许您选择Unix时间单位。对于我正在研究的项目,我们使用“ ms”,但是如果需要,您可以指定其他单位。

此外,这允许使用numpy将datetime64数组的摄取转换为timedelta64:

>>> import numpy as np
>>> now = np.datetime64('now')
>>> ux_time = now.astype(np.timedelta64) / np.timedelta64(1, 'ms')

我用它来将从熊猫中提取的np.datetime64列转换为unixtime数组

答案 5 :(得分:-1)

def get_unixtime(time):    
    return (time.astype(np.int64)/1e6).astype(np.int64)
get_unixtime(np.datetime64('now'))

似乎返回UNIX时间戳,我只检查过几个日期。