【问题标题】:convertion of datetime to numpy datetime without timezone info没有时区信息的日期时间到 numpy 日期时间的转换
【发布时间】:2015-04-13 22:41:49
【问题描述】:

假设我有一个datetime 变量:

     dt = datetime.datetime(2001,1,1,0,0)

然后我将其转换为 numpy 如下numpy.datetime64(dt) 我得到

     numpy.datetime64('2000-12-31T19:00:00.000000-0500')

dtype('<M8[us]')

但这会自动考虑我的时区(即本例中的 EST),并返回 2001 年 12 月 31 日的日期和 19:00 小时的时间。

如何在 numpy 中将其转换为datetime64[D],忽略时区信息并简单地给我

     numpy.datetime64('2001-01-01')

dtype('<M8[D]')

The numpy datetime64 doc page 没有提供有关如何忽略时区或将默认时区设置为 UTC 的信息

【问题讨论】:

    标签: python-3.x numpy python-datetime


    【解决方案1】:

    前几天我只是在玩这个。我认为有两个问题——datetime.datetime 对象如何转换为np.datetime64,以及后者如何显示。

    numpy 文档讨论了从日期字符串创建datatime64 对象。看来,当给定一个datetime.datetime 对象时,它首先会产生一个字符串。

    np.datetime64(dt) == np.datetime64(dt.isoformat())
    

    我发现我可以在该字符串中添加时区信息

    np.datetime64(dt.isoformat()+'Z')  # default assumption
    np.datetime64(dt.isoformat()+'-0500')
    

    Numpy 1.7.0 将不带 TZ 的 ISO 8601 字符串读取为本地(ISO 指定此)

    日期时间始终基于 POSIX 时间存储,纪元为 1970-01-01T00:00Z

    至于显示,test_datetime.py 文件提供了一些关于未记录行为的线索。

    https://github.com/numpy/numpy/blob/280f6050d2291e50aeb0716a66d1258ab3276553/numpy/core/tests/test_datetime.py

    例如:

    def test_datetime_array_str(self):
            a = np.array(['2011-03-16', '1920-01-01', '2013-05-19'], dtype='M')
            assert_equal(str(a), "['2011-03-16' '1920-01-01' '2013-05-19']")
    
            a = np.array(['2011-03-16T13:55Z', '1920-01-01T03:12Z'], dtype='M')
            assert_equal(np.array2string(a, separator=', ',
                        formatter={'datetime': lambda x :
                                "'%s'" % np.datetime_as_string(x, timezone='UTC')}),
                         "['2011-03-16T13:55Z', '1920-01-01T03:12Z']")
    

    因此您可以使用np.array2stringnp.datetime_as_string 自定义数组的打印行为。 np.set_printoptions 也接受 formatter 参数。

    pytz 模块用于添加进一步的时区处理:

     @dec.skipif(not _has_pytz, "The pytz module is not available.")
        def test_datetime_as_string_timezone(self):
            # timezone='local' vs 'UTC'
            a = np.datetime64('2010-03-15T06:30Z', 'm')
            assert_equal(np.datetime_as_string(a, timezone='UTC'),
                    '2010-03-15T06:30Z')
            assert_(np.datetime_as_string(a, timezone='local') !=
                    '2010-03-15T06:30Z')
            ....
    

    例子:

    In [48]: np.datetime_as_string(np.datetime64(dt),timezone='local')
    Out[48]: '2000-12-31T16:00:00.000000-0800'
    
    In [49]: np.datetime64(dt)
    Out[49]: numpy.datetime64('2000-12-31T16:00:00.000000-0800')
    
    In [50]: np.datetime_as_string(np.datetime64(dt))
    Out[50]: '2001-01-01T00:00:00.000000Z'
    
    In [51]: np.datetime_as_string(np.datetime64(dt),timezone='UTC')
    Out[51]: '2001-01-01T00:00:00.000000Z'
    
    In [52]: np.datetime_as_string(np.datetime64(dt),timezone='local')
    Out[52]: '2000-12-31T16:00:00.000000-0800'
    
    In [81]: np.datetime_as_string(np.datetime64(dt),timezone=pytz.timezone('US/Eastern'))
    Out[81]: '2000-12-31T19:00:00.000000-0500'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 2019-11-18
      相关资源
      最近更新 更多