&引用;属性错误:';矩阵&x27;对象没有属性';isocalendar'&引用;在numpython中

&引用;属性错误:';矩阵&x27;对象没有属性';isocalendar'&引用;在numpython中,python,numpy,matrix,Python,Numpy,Matrix,我有一个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

我有一个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
将矩阵转换为具有以下内容的数据帧:

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.values
df.as\u matrix()
获得一个数组。即使需要使用新的包,也希望这能有所帮助。

有两个问题:1)将字符串转换为datetime对象,2)迭代
矩阵的元素

您给了我们一个包含字符串的矩阵:

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

In [286]: dates
Out[286]: 
matrix([['09/01/70'],
        ['10/01/70'],
        ['11/01/70']], 
       dtype='|S8')
矩阵
和字符串都没有日期时间功能。这些字符串必须单独转换

def getweek(dstr):
    return datetime.datetime.strptime(dstr,"%d/%m/%y").isocalendar()[1]

In [288]: getweek("15/10/15")
Out[288]: 42
将其应用于
矩阵
有点麻烦,因为我必须为行和列编制索引

只是要弄清楚发生了什么,让我们在矩阵上迭代并返回日期字符串和星期

In [285]: [(x[0,0],getweek(x[0,0])) for x in dates]                    
Out[285]: [('09/01/70', 2), ('10/01/70', 2), ('11/01/70', 2)]
getweek
可以根据您对其他日期问题的回答中的建议进行调整,以便与
apply_沿_轴
一起工作

您使用
np.matrix
是否有特殊原因?为什么不
np.array
matrix
的一个问题是,它不断返回另一个矩阵-另一个2d对象,即使它只有一个元素

有一种很好的方法可以将其转换为一维阵列:

In [322]: dates.A1
Out[322]: 
array(['09/01/70', '10/01/70', '11/01/70'], 
      dtype='|S8')
In [293]: dates = np.array(['09/01/70', '10/01/70', '11/01/70'])
In [294]: [(x,getweek(x)) for x in dates]
Out[294]: [('09/01/70', 2), ('10/01/70', 2), ('11/01/70', 2)]
In [295]: np.array([getweek(x) for x in dates])
Out[295]: array([2, 2, 2])
使用1d阵列:

In [322]: dates.A1
Out[322]: 
array(['09/01/70', '10/01/70', '11/01/70'], 
      dtype='|S8')
In [293]: dates = np.array(['09/01/70', '10/01/70', '11/01/70'])
In [294]: [(x,getweek(x)) for x in dates]
Out[294]: [('09/01/70', 2), ('10/01/70', 2), ('11/01/70', 2)]
In [295]: np.array([getweek(x) for x in dates])
Out[295]: array([2, 2, 2])
像这样的日期字符串通常来自csv文件。我们可以使用
genfromtxt
getweek
加载它们

使用文本行模拟文件,并为
genfromtxt
定义
converters

In [339]: txt=b"""09/01/70
   .....: 10/02/70
   .....: 11/03/70
   .....: """

In [340]: np.genfromtxt(txt.splitlines(),dtype=None,converters={0:getweek})
Out[340]: array([ 2,  7, 11])

这里有一种使用
np.datetime64
dtype获取周数的方法

In [350]: txt=b"""1970-09-01
1970-10-15
1970-11-25
1970-01-10"""

In [351]: d=np.genfromtxt(txt.splitlines(),dtype='datetime64[D]')
# load the dates as Days; yyyy-mm-dd is the default format

In [352]: d
Out[352]: array(['1970-09-01', '1970-10-15', '1970-11-25', '1970-01-10'], dtype='datetime64[D]')

In [353]: d.astype('datetime64[W]')-d.astype('datetime64[Y]')    
Out[353]: array([34, 41, 46,  1], dtype='timedelta64[W]')
d.astype('datetime64[W]”)
将日单位转换为周,尽管它显示为一周的第一天

d.astype('datetime64[Y]”
-是年单位,但显示为年初


它们的差异是周数(0是一年中第一周的一天)。可能有一种更简洁的推导方法,但我觉得这种方法很有启发性。

你不能应用你刚才得到的答案吗?
dates
是一个矩阵,而不是
datetime
对象。是的,谢谢。我想我可以应用。我已经做了类似的事情,但它不起作用。np.沿着轴应用((lambda x:[x[0].isocalendar()[1] )1,日期)你是将
isocalendar
应用于
datetime
对象还是应用于数字?hpaulj,谢谢你的长篇大论。问题是,你解释了一个长篇大论的有益答案,但没有解释如何将其与我的答案相匹配。我有一个列矩阵,我想得到一个列矩阵的结果。
np.matrix([…]).T
?没有“for”就没有其他方法了像lambda?因为for循环需要很多时间。它会按照我的要求提供输出,但解决方案很长,CPU处理需要很长时间。我有很多这样的数组,我不想等待太长时间才能找到解决方案。你在
沿u轴应用
框架中尝试过吗?