假设我有一个由一年中所有小时数组成的数组,例如
np.arange(8760)
Out[5]: array([ 0, 1, 2, ..., 8757, 8758, 8759])
下面的代码可以处理从一年中的小时数索引一天
year = 2012
month = 1
day = 1
fmt ='%Y-%m-%d_%H:%M:%S'
day0 = datetime.datetime(year,month,day)
new_time = []
index_date = []
time_list=(np.arange(8760)).tolist()
####actually the timelist is a column of other array
for hour in time_list:
delta = datetime.timedelta(hours=hour)
out_date = datetime.datetime.strftime(day0+delta,fmt)
new_time.append(out_date)
for_index_date = datetime.datetime.strftime(day0 + delta, "%Y-%m-%d")
index_date.append(for_index_date)
new_time = np.array(new_time) #for Times of ncfile
index_date=np.array(index_date) #for indexing of days
ntim = 24 ## 1day = 24 hour
### I want to index multiple days
index = np.where(index_date=="2012-09-01")
new_time=new_time[index]
Times = np.zeros((ntim,19),dtype="|S1")
Times[:]=new_time.view('U1').reshape(new_time.size, -1).astype('S1')
但是,如果我想把一天从2012-09-01
索引到2012-09-05
,这意味着ntim=120
,我不知道如何使np.where()
工作。你知道吗
有没有什么有效的方法可以根据一年中的小时数来获取datetime和date,然后用np.where("2012-09-05">=index_date>="2012-09-01")
之类的方法进行索引
任何帮助都将不胜感激。你知道吗
考虑使用numpy的内置
datetime64
和timedelta64
:或者反过来说:
相关问题 更多 >
编程相关推荐