" AttributeError:“矩阵"对象没有属性"strftime"" numpy python中的错误 [英] "AttributeError: 'matrix' object has no attribute 'strftime'" error in numpy python

查看:224
本文介绍了" AttributeError:“矩阵"对象没有属性"strftime"" numpy python中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(72000,1)维的矩阵.该矩阵涉及时间戳.

I have a matrix with (72000, 1) dimension. This matrix involves timestamps.

我想使用"strftime"如下: strftime("%d/%m/%y"),以便获得类似以下内容的输出:'11/03/02'.

I want to use "strftime" as the following; strftime("%d/%m/%y"), in order to get the output something like this: '11/03/02'.

我有这样一个矩阵:

M = np.matrix([timestamps])

并且我已经使用"strftime"来将所有包含时间戳的矩阵转换为包含字符串类型中的日期的矩阵.因此,我使用"strftime"作为后记:

And I have used "strftime" in order to convert all the matrix involving timestamps to a matrix involving dates in string types. For this reason, I have used "strftime" as the follwing:

M = M.strftime("%d/%m/%y")

运行代码时,出现此错误:

When I run the code, I get this error:

AttributeError: 'matrix' object has no attribute 'strftime'

使用此功能的正确方法是什么?如何将时间戳矩阵转换为日期字符串矩阵?

What is the right way of using this function? How can I convert the timestamp matrix to date string matrix?

推荐答案

如错误消息所示,您不能执行matrix.strftime之类的操作.您可以做的一件事是使用 numpy.apply_along_axis .示例-

As the error message shows you, you cannot do something like matrix.strftime . One thing you can do would be to use numpy.apply_along_axis . Example -

np.apply_along_axis((lambda x:[x[0].strftime("%d/%m/%y")]),1,M)

演示-

In [58]: M = np.matrix([[datetime.datetime.now()]*5]).T

In [59]: M.shape
Out[59]: (5, 1)

In [60]: np.apply_along_axis((lambda x:[x[0].strftime("%d/%m/%y")]),1,M)
Out[60]:
array([['10/10/15'],
       ['10/10/15'],
       ['10/10/15'],
       ['10/10/15'],
       ['10/10/15']],
      dtype='<U8')


对于新错误,您会遇到-


For the new error you are getting -

"AttributeError:'numpy.float64'对象没有属性'strftime'"

"AttributeError: 'numpy.float64' object has no attribute 'strftime'"

这意味着对象不是datetime对象,因此,如果它们是时间戳记,则可以先将它们转换为日期时间.示例-

This means that the objects are not datetime objects, so if they are timestamps , you can converting them to datetime first. Example -

np.apply_along_axis((lambda x:[datetime.datetime.fromtimestamp(x[0]).strftime("%d/%m/%y")]),1,M)

这篇关于&quot; AttributeError:“矩阵"对象没有属性"strftime"" numpy python中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆