gpt4 book ai didi

Python numpy where 函数与日期时间

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:38 32 4
gpt4 key购买 nike

我有 30 年来每日日期时间的数组。我正在尝试使用 np.where 按月和日过滤此数组,以便我可以找到一年中每一天相应数据条目的平均值。

但是,我收到错误:

AttributeError: 'list' object has no attribute 'month'

当我这样做时,我可以正确查询数据:

print "dates[100].month: ", dates[100].month

有什么方法可以使用 numpy where 函数来查询 datetime 条目吗?

到目前为止我的代码是:

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是一个列表,因此它不能用作datetime,即使它的元素是datetime > 类型。所以而不是

dates.month == i

你需要类似的东西

[d.month == i for d in months]

关于Python numpy where 函数与日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24224138/

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