简体   繁体   English

numpy python中的“ AttributeError:'matrix'对象没有属性'strftime'”错误

[英]“AttributeError: 'matrix' object has no attribute 'strftime'” error in numpy python

I have a matrix with (72000, 1) dimension. 我有一个尺寸为(72000,1)的矩阵。 This matrix involves timestamps. 该矩阵涉及时间戳。

I want to use "strftime" as the following; 我想使用“ strftime”如下: strftime("%d/%m/%y") , in order to get the output something like this: '11/03/02' . strftime("%d/%m/%y") ,以获取如下输出: '11/03/02'

I have such a matrix: 我有这样一个矩阵:

M = np.matrix([timestamps])

And I have used "strftime" in order to convert all the matrix involving timestamps to a matrix involving dates in string types. 为了将所有包含时间戳的矩阵转换为包含字符串类型中的日期的矩阵,我使用了“ strftime”。 For this reason, I have used "strftime" as the follwing: 出于这个原因,我将“ strftime”用作了以下用法:

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? 如何将时间戳矩阵转换为日期字符串矩阵?

As the error message shows you, you cannot do something like matrix.strftime . 如错误消息所示,您不能执行诸如matrix.strftime类的matrix.strftime One thing you can do would be to use numpy.apply_along_axis . 您可以做的一件事是使用numpy.apply_along_axis Example - 范例-

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

Demo - 演示-

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' object has no attribute 'strftime'" “ AttributeError:'numpy.float64'对象没有属性'strftime'”

This means that the objects are not datetime objects, so if they are timestamps , you can converting them to datetime first. 这意味着这些对象不是datetime对象,因此,如果它们是时间戳记,则可以先将它们转换为日期时间。 Example - 范例-

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

暂无
暂无

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

相关问题 AttributeError: &#39;str&#39; 对象在 python 中没有属性 &#39;strftime&#39; - AttributeError: 'str' object has no attribute 'strftime' in python numpy python中的“ AttributeError:&#39;matrix&#39;对象没有属性&#39;isocalendar&#39;” - “AttributeError: 'matrix' object has no attribute 'isocalendar'” in numpy python 日期列表,Python - [AttributeError: 'list' object has no attribute 'strftime'] - List of dates, Python - [AttributeError: 'list' object has no attribute 'strftime'] AttributeError: 'str' object 在 django 中没有属性 'strftime' - AttributeError: 'str' object has no attribute 'strftime' in django AttributeError:&#39;str&#39;对象没有属性&#39;strftime&#39; - AttributeError: 'str' object has no attribute 'strftime' python:如何将数组转换为矩阵? 出现错误:AttributeError: 'matrix' object 没有属性 'adjugate' - python: how to convert an array into a matrix? with error: AttributeError: 'matrix' object has no attribute 'adjugate' AttributeError: &#39;tuple&#39; 对象在 postgres 上没有属性 &#39;strftime&#39; 问题 - AttributeError: 'tuple' object has no attribute 'strftime' issue on postgres 为什么我得到 AttributeError: 'str' object has no attribute 'strftime'? - why I am getting AttributeError: 'str' object has no attribute 'strftime'? AttributeError:修改pandas数据帧时,&#39;str&#39;对象没有属性&#39;strftime&#39; - AttributeError: 'str' object has no attribute 'strftime' when modifying pandas dataframe Python“AttributeError:&#39;NoneType&#39;对象没有属性”错误 - Python "AttributeError: 'NoneType' object has no attribute" Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM