Python 在numpy数组中查找大于输入日期时间的条目
我希望返回的datetime numpy数组的条目比输入的datetime变量大 不幸的是,我在执行以下代码时遇到了此错误:Python 在numpy数组中查找大于输入日期时间的条目,python,numpy,Python,Numpy,我希望返回的datetime numpy数组的条目比输入的datetime变量大 不幸的是,我在执行以下代码时遇到了此错误: TypeError: '>' not supported between instances of 'int' and 'datetime.datetime' 这是我的代码: import numpy as np import pandas as pd myRange = pd.date_range('2018-04-09', periods=5, freq='1
TypeError: '>' not supported between instances of 'int' and 'datetime.datetime'
这是我的代码:
import numpy as np
import pandas as pd
myRange = pd.date_range('2018-04-09', periods=5, freq='1D20min')
myArray = np.array(myRange).astype(np.datetime64).reshape(-1,1)
print("myArray:", myArray)
myDatetime = pd.datetime(2018,4,10,2,59,59)
myArray[myArray>myDatetime]
问题在于比较:
带有
myDateTime(类型为pd.datetime) 将myDateTime更改为numpy datetime64将给出一个结果
import numpy as np
import pandas as pd
myRange = pd.date_range('2018-04-09', periods=5, freq='1D20min')
myArray = np.array(myRange).astype(np.datetime64).reshape(-1,1)
print("myArray:", myArray)
myDatetime = np.datetime64("2018-04-10T02:59:59")
myArray[myArray>myDatetime]
给出:
myArray: [['2018-04-09T00:00:00.000000000']
['2018-04-10T00:20:00.000000000']
['2018-04-11T00:40:00.000000000']
['2018-04-12T01:00:00.000000000']
['2018-04-13T01:20:00.000000000']]
Out[27]:
array(['2018-04-11T00:40:00.000000000',
'2018-04-12T01:00:00.000000000',
'2018-04-13T01:20:00.000000000'], dtype='datetime64[ns]')
您试图使用布尔索引,但语法不正确。
myArray: [['2018-04-09T00:00:00.000000000']
['2018-04-10T00:20:00.000000000']
['2018-04-11T00:40:00.000000000']
['2018-04-12T01:00:00.000000000']
['2018-04-13T01:20:00.000000000']]
Out[27]:
array(['2018-04-11T00:40:00.000000000',
'2018-04-12T01:00:00.000000000',
'2018-04-13T01:20:00.000000000'], dtype='datetime64[ns]')