简体   繁体   English

在Numpy 1.6.1中将float32数组转换为datetime64

[英]Convert float32 array to datetime64 in Numpy 1.6.1

What is the proper way of converting integer dates to datetime64 in numpy? 在numpy中将整数日期转换为datetime64的正确方法是什么? I tried: 我试过了:

import numpy
a = numpy.array([20090913, 20101020, 20110125])
numpy.datetime64(a.astype("S8"))

but get an incorrect conversion. 但得到的转换不正确。 How about reading them in correctly as numpy.datetime64 objects using numpy.loadtxt (they are coming from a csv file)? 如何使用numpy.loadtxt(它们来自csv文件)正确读取numpy.datetime64对象?

You problem is that datetime64 expects a string in the format yyyy-mm-dd , while the type conversion produces strings in the format yyyymmdd . 您的问题是datetime64需要格式为yyyy-mm-dd字符串,而类型转换会生成格式为yyyymmdd字符串。 I would suggest something like this: 我会建议这样的事情:

conversion = lambda x: "%s-%s-%s" % (x[:4], x[4:6], x[6:])
np_conversion = numpy.frompyfunc(conversion,1,1)
b = np_conversion(a.astype('S10'))
numpy.datetime64(b)

However it's not working for me (I have numpy 1.6.1), it fails with the message "NotImplementedError: Not implemented for this type". 但是它对我不起作用(我有numpy 1.6.1),它失败并显示消息“NotImplementedError:Not implemented for this type”。 Unless that is implemented in 1.7, I can only suggest a pure Python solution: 除非在1.7中实现,否则我只能建议一个纯Python解决方案:

numpy.datetime64(numpy.array([conversion(str(x)) for x in a], dtype="S10"))

...or pre-processing your input, to deliver the dates in the expected format. ...或预处理您的输入,以预期的格式提供日期。

Edit: I can also offer an alternative solution, using vectorize , but I don't know very well how it works, so I don't know what's going wrong: 编辑:我也可以使用vectorize提供替代解决方案,但我不太清楚它是如何工作的,所以我不知道出了什么问题:

>>> conversion = vectorize(lambda x: "%s-%s-%s" % (x[:4], x[4:6], x[6:]), otypes=['S10'])
>>> conversion(a.astype('S10'))
array(['2009', '2010', '2011'],
      dtype='|S4')

For some reason it's ignoring the otypes and outputting |S4 instead of |S10 . 由于某种原因,它忽略了otypes并输出|S4而不是|S10 Sorry I can't help more, but this should provide a starting point for searching other solutions. 对不起,我无能为力,但这应该是搜索其他解决方案的起点。

Update: Thanks to OP feedback, I thought of a new possibility. 更新:感谢OP反馈,我想到了一个新的可能性。 This should work as expected: 这应该按预期工作:

>>> conversion = lambda x: numpy.datetime64(str(x))
>>> np_conversion = numpy.frompyfunc(conversion, 1, 1)
>>> np_conversion(a)
array([2009-09-13 00:00:00, 2010-10-20 00:00:00, 2011-01-25 00:00:00], dtype=object)

# Works too:
>>> conversion = lambda x: numpy.datetime64("%s-%s-%s" % (x/10000, x/100%100, x%100))

Weird how, in this case, datetime64 works fine with or without the dashes... 奇怪的是,在这种情况下, datetime64在有或没有短划线的情况下工作正常......

Oddly, this works: numpy.datetime64(a.astype("S8").tolist()) , while this does not: numpy.datetime64(a.astype("S8")) . 奇怪的是,这有效: numpy.datetime64(a.astype("S8").tolist()) ,而这不是: numpy.datetime64(a.astype("S8")) The first method is still a bit less convoluted than: numpy.array([numpy.datetime64(str(i)) for i in a]) . 第一种方法仍然比以下方法更复杂: numpy.array([numpy.datetime64(str(i)) for i in a]) I asked why in this question . 我问这个问题为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM