【问题标题】:How to construct a numpy array with multilple same timestamp?如何构造具有多个相同时间戳的numpy数组?
【发布时间】:2016-11-01 23:05:19
【问题描述】:

我试过了:

import numpy as np
t = np.full((10),'2012-12-10', dtype=np.datetime64)

但是遇到了这样的错误:

ValueError: Cannot create a NumPy datetime other than NaT with generic units

你有什么想法吗?谢谢!

【问题讨论】:

    标签: python arrays numpy timestamp


    【解决方案1】:

    编辑:我想通了,您必须完全指定日期时间对象的数据类型

    这样做

    np.full((10), '2012-12-10', dtype='datetime64[D]')
    
    array(['2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
           '2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
           '2012-12-10', '2012-12-10'], dtype='datetime64[D]')
    

    下面是我之前的回答


    嗯,不知道为什么 np.full 在这种情况下特别不起作用。然而,实现这一目标的一种方法是改用np.tile

    np.tile(np.array(['2012-12-10'], dtype=np.datetime64), 10)
    
    array(['2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
           '2012-12-10', '2012-12-10', '2012-12-10', '2012-12-10',
           '2012-12-10', '2012-12-10'], dtype='datetime64[D]')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      相关资源
      最近更新 更多