gpt4 book ai didi

python - numpy python 中的 "AttributeError: ' 矩阵 ' object has no attribute ' 等历 '"

转载 作者:太空宇宙 更新时间:2023-11-04 01:00:29 25 4
gpt4 key购买 nike

我有一个如下所示的 numpy 矩阵:

dates = np.matrix([['09/01/70'], ['10/01/70'], ['11/01/70']])

我想将此矩阵转换为涉及周数而不是日期的矩阵。

感谢 python 中的“isocalendar()”函数,您可以找到日期的周数。

但是当我使用如下函数时;

weeks = dates.isocalendar()[1],

我收到这个错误:

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

在 numpy python 中将日期矩阵转换为日期数字矩阵的正确方法是什么?

最佳答案

在这种情况下,我会使用 pandas,将矩阵转换为 DataFrame:

df = pd.DataFrame(dates)

然后我将日期转换为 datetime 对象:

df[0] = pd.to_datetime(df[0])

最后应用 isocalendar() 函数:

df = df[0].apply(lambda x: x.isocalendar()[1])

返回:

0    36
1 40
2 44
Name: 0, dtype: int64

然后您可以使用 df.valuesdf.as_matrix() 获得一个数组。希望即使它需要使用新包也能有所帮助。

关于python - numpy python 中的 "AttributeError: ' 矩阵 ' object has no attribute ' 等历 '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com