Python 无法在 numpy.datetime64 上调用 strftime,没有定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28327101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't call strftime on numpy.datetime64, no definition
提问by deeb
I have a datetime64 t
that I'd like to represent as a string.
我有一个 datetime64 t
,我想表示为一个字符串。
When I call strftime like this t.strftime('%Y.%m.%d')
I get this error:
当我像这样调用 strftime 时,t.strftime('%Y.%m.%d')
我收到此错误:
AttributeError: 'numpy.datetime64' object has no attribute 'strftime'
What am I missing? I am using Python 3.4.2 and Numpy 1.9.1
我错过了什么?我正在使用 Python 3.4.2 和 Numpy 1.9.1
采纳答案by user 12321
Use this code:
使用此代码:
import pandas as pd
t= pd.to_datetime(str(date))
timestring = t.strftime('%Y.%m.%d')
回答by apteryx
Importing a data structures library like pandas to accomplish type conversion feels like overkill to me. You can achieve the same thing with the standard datetime module:
导入像 Pandas 这样的数据结构库来完成类型转换对我来说有点矫枉过正。您可以使用标准 datetime 模块实现相同的功能:
import numpy as np
import datetime
t = np.datetime64('2017-10-26')
t = t.astype(datetime.datetime)
timestring = t.strftime('%Y.%m.%d')
回答by John Zwinck
This is the simplest way:
这是最简单的方法:
t.item().strftime('%Y.%m.%d')
item()
gives you a Python native datetime object, on which all the usual methods are available.
item()
为您提供一个 Python 本机日期时间对象,在该对象上可以使用所有常用方法。
回答by David Wasserman
If your goal is only to represent t
as a string, the simplest solution is str(t)
. If you want it in a specific format, you should use one of the solutions above.
如果您的目标只是表示t
为字符串,那么最简单的解决方案是str(t)
. 如果您希望采用特定格式,则应使用上述解决方案之一。
One caveat is that np.datetime64
can have different amounts of precision. If t has nanosecond precision, user 12321's solution will still work, but apteryx's and John Zwinck's solutions won't, because t.astype(datetime.datetime)
and t.item()
return an int
:
一个警告是np.datetime64
可以有不同的精度。如果 t 具有纳秒精度,用户 12321 的解决方案仍然有效,但 apteryx 和 John Zwinck 的解决方案不会,因为t.astype(datetime.datetime)
并t.item()
返回一个int
:
import numpy as np
print('second precision')
t = np.datetime64('2000-01-01 00:00:00')
print(t)
print(t.astype(datetime.datetime))
print(t.item())
print('microsecond precision')
t = np.datetime64('2000-01-01 00:00:00.0000')
print(t)
print(t.astype(datetime.datetime))
print(t.item())
print('nanosecond precision')
t = np.datetime64('2000-01-01 00:00:00.0000000')
print(t)
print(t.astype(datetime.datetime))
print(t.item())
import pandas as pd
print(pd.to_datetime(str(t)))
second precision
2000-01-01T00:00:00
2000-01-01 00:00:00
2000-01-01 00:00:00
microsecond precision
2000-01-01T00:00:00.000000
2000-01-01 00:00:00
2000-01-01 00:00:00
nanosecond precision
2000-01-01T00:00:00.000000000
946684800000000000
946684800000000000
2000-01-01 00:00:00
回答by adrianp
For those who might stumble upon this: numpy now has a numpy.datetime_as_stringfunction. Only caveat is that it accepts an array rather than just an individual value. I could make however that this is still a better solution than having to use another library just to do the conversion.
对于那些可能会偶然发现这一点的人:numpy 现在有一个numpy.datetime_as_string函数。唯一需要注意的是它接受一个数组而不仅仅是一个单独的值。然而,我可以说这仍然是一个比使用另一个库来进行转换更好的解决方案。