我正在关注senddex编写的有关将UNIX时间戳转换为matplotlib识别的日期的教程。我仍然是新手,虽然我理解文档中给出的示例,因为它们非常简单,但我不明白为什么在这种情况下使用np.vectorize:
date_convert = np.vectorize(dt.datetime.fromtimestamp)
date = date_convert(date)
任何帮助将不胜感激!感谢您的耐心配合!
什么np.vectorize
做主要是做datetime
功能上的数组,而不是单一的值可用。
如果使用的dt.datetime.fromtimestamp
话,那是正确的,它仅接受1个参数,但也仅对1个值起作用。因此,以下具有2个日期的示例将显示一个错误。
import datetime as dt
import numpy as np
dates = np.array([13444,9000009])
dt.datetime.fromtimestamp(dates)
TypeError:只有大小为1的数组可以转换为Python标量
但是,usingnp.vectorize
创建了一个称为的新函数date_convert
,该函数执行的功能与dt.datetime.fromtimestamp
允许数组中的多个值相同。
date_convert = np.vectorize(dt.datetime.fromtimestamp)
dates = np.array([13444,9000009])
date_convert(dates)
#returns:
#array([datetime.datetime(1969, 12, 31, 20, 44, 4),
# datetime.datetime(1970, 4, 14, 21, 0, 9)], dtype=object)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句