arrays - 在 Numpy Array Python 中获取日期时间索引
问题描述
我怎样才能编写一个代码来显示我所在位置的Newdate1 and Newdate2
索引Setups
。Newdate1
within的值Setups
是输出1
for的第二个索引result
。但是该np.where
功能不起作用。如果没有 for 循环,我怎么能做到这一点?
import numpy as np
Setups = np.array(['2017-09-15T07:11:00.000000000', '2017-09-15T11:25:00.000000000',
'2017-09-15T12:11:00.000000000', '2017-12-22T03:14:00.000000000',
'2017-12-22T03:26:00.000000000', '2017-12-22T03:31:00.000000000',
'2017-12-22T03:56:00.000000000'],dtype="datetime64[ns]")
Newdate1 = np.array(['2017-09-15T07:11:00.000000000'], dtype="datetime64[ns]")
Newdate2 = np.array(['2017-12-22T03:26:00.000000000'], dtype="datetime64[ns]")
result = np.where(Setups == Newdate1)
result2 = np.where(Setups == Newdate2)
预期输出:
result: 1
result2: 4
解决方案
用于np.in1d
传递要在另一个数组中搜索的数组并使用np.where
.
import numpy as np
Setups = np.array(['2017-09-15T07:11:00.000000000', '2017-09-15T11:25:00.000000000',
'2017-09-15T12:11:00.000000000', '2017-12-22T03:14:00.000000000',
'2017-12-22T03:26:00.000000000', '2017-12-22T03:31:00.000000000',
'2017-12-22T03:56:00.000000000'],dtype="datetime64[ns]")
newdates = np.array(['2017-09-15T07:11:00.000000000','2017-12-22T03:26:00.000000000'],dtype="datetime64[ns]")
print(np.where(np.in1d(Setups,newdates)))
输出:
(array([0, 4]),)
推荐阅读
- python - pip install SpeechRecognition 失败
- azure-devops - Azure DevOps 中的条件变量值在使用变量组时构建管道
- android - BaseAdapter 的自定义对话框回收器视图未更新 andorid
- c# - 从继承的类创建实例时出现问题,C# unity
- php - Instagram ?__a=1 json 获取第 2 页
- android - 如何从广播接收器中的 Firebase 实时数据库中获取数据并在日常通知中使用它们?
- unity3d - 错误:Assets/beandefine.cs(23,1)(抱歉 Dani)类型或命名空间定义,或预期文件结尾
- javascript - javascript .then 不执行
- javascript - 在 Google 表格脚本中找不到错误表格
- php - 在 Woocoomerce 中显示订单信息的简码不起作用