Python Numpy的哪里有日期时间功能 [英] Python numpy where function with datetime
本文介绍了Python Numpy的哪里有日期时间功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列30年的每日约会时间.我试图使用np.where
按月和日过滤此数组,以便可以找到一年中每一天对应数据条目的平均值.
I have an array of daily date times over a period of 30 years. I am trying to filter this array by month and day using np.where
so that I can find the average of the corresponding data entries for each day of the year.
但是,我得到了错误:
AttributeError: 'list' object has no attribute 'month'
我可以在执行以下操作时正确查询数据:
I can query the data correctly when I do:
print "dates[100].month: ", dates[100].month
我可以使用numpy
where
函数查询datetime
条目吗?
Is there any way I can use the numpy
where
function to query datetime
entries?
到目前为止,我的代码是:
The code I have so far is:
clim = np.zeros((12,31))
m_arr = range(1,13) # months array
d_arr = range(1,32) # days array
for i in range(len(m_arr)):
for j in range(len(d_arr)):
# finding indexes of the dates array
tt, = np.where((dates.month == i) & (dates.day == j))
data2 = np.zeros(len(tt))
dates2 = np.zeros(len(tt))
if len(tt) > 0:
for num in range(len(tt)):
site = tt[num]
dates2[num] = dates[site]
data2[num] = data[site]
clim[i,j]=data2[~np.isnan(data2)].mean() # some nan entries in data
推荐答案
由于dates
是 list ,因此即使其元素为datetime
,也不能将其用作datetime
.类型.因此,而不是
Since dates
is a list, it cannot be used as a datetime
, even if its elements are datetime
type. So instead of
dates.month == i
您需要类似的东西
[d.month == i for d in months]
这篇关于Python Numpy的哪里有日期时间功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文