Python numpy where 函数与日期时间

标签 python arrays datetime numpy

我有 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/

相关文章:

python - python 上的 cprofile 编译错误

javascript - 在 JavaScript 中查找多维数组中最长的字符串

excel - 根据列中的值有条件地设置一个单元格的格式

java - 在java中计算两个给定时间之间的差异

r - POSIXlt 组件的提取在 R 3.4.4 中运行良好,但在 R 3.5.0 中出现错误。为什么?

python - 具有国际特色的完成者

python - 在 numpy 数组中以不规则的时间间隔加速移动时间增量

python - Databricks - pyspark.pandas.Dataframe.to_excel 无法识别 abfss 协议(protocol)

python - 在Python中将300个CSV文件转换为一个二维数组

C - 通过管道发送结构数组