'numpy.timedelta64'对象不可迭代


import numpy as np
base=dsloc.time.values
time=np.array([base+np.timedelta64(step) for step in dsloc.step.values])

我试着使用时间序列https://github.com/enyfeo/efas/blob/master/work/5_Timeseries.ipynb

我在指定的行中出现以下错误;TypeError:"numpy.timedelta64"对象不是可迭代的

你能帮我吗?谢谢

编辑;

import pandas as pd
import xarray as xr
import numpy as np
from random import sample
#%matplotlib notebook
import matplotlib
import matplotlib.pyplot as plot
pd.plotting.register_matplotlib_converters()
stations = pd.read_excel('C:/Users/90531/Desktop/Lisflood/KONYA_LONG_LAT_4digit.xlsx')
#station = stations.sample(n=1) # We can randomly choose a station
station=stations[stations['stname'] == 300 ] # We have chosen a station for consistency
station

这对我有用:

import pandas as pd
from random import sample
stations = pd.read_excel('KONYA_LONG_LAT_4digit.xlsx')
#station = stations.sample(n=1) # We can randomly choose a station
station=stations[stations['stname'] == 300 ] # We have chosen a station for consistency

import xarray as xr
ds = xr.open_dataset('snow.nc')

# extract data for selected point in netcdf file by LISFLOOD coordinates
dsloc = ds.sel(x=station.lat.values,y=station.long.values,method='nearest')

import numpy as np
base=dsloc.time.values
time=np.array([base+np.timedelta64(step) for step in dsloc.step.values])
print(time)

输出:

['2019-04-01T00:00:00.000000000' '2019-04-01T06:00:00.000000000'
'2019-04-01T12:00:00.000000000' '2019-04-01T18:00:00.000000000'
'2019-04-02T00:00:00.000000000' '2019-04-02T06:00:00.000000000'
'2019-04-02T12:00:00.000000000' '2019-04-02T18:00:00.000000000'
'2019-04-03T00:00:00.000000000' '2019-04-03T06:00:00.000000000'
'2019-04-03T12:00:00.000000000' '2019-04-03T18:00:00.000000000'
'2019-04-04T00:00:00.000000000' '2019-04-04T06:00:00.000000000'
'2019-04-04T12:00:00.000000000' '2019-04-04T18:00:00.000000000'
'2019-04-05T00:00:00.000000000' '2019-04-05T06:00:00.000000000'
'2019-04-05T12:00:00.000000000' '2019-04-05T18:00:00.000000000'
'2019-04-06T00:00:00.000000000' '2019-04-06T06:00:00.000000000'
'2019-04-06T12:00:00.000000000' '2019-04-06T18:00:00.000000000'
'2019-04-07T00:00:00.000000000' '2019-04-07T06:00:00.000000000'
'2019-04-07T12:00:00.000000000' '2019-04-07T18:00:00.000000000'
'2019-04-08T00:00:00.000000000' '2019-04-08T06:00:00.000000000'
'2019-04-08T12:00:00.000000000' '2019-04-08T18:00:00.000000000'
'2019-04-09T00:00:00.000000000' '2019-04-09T06:00:00.000000000'
'2019-04-09T12:00:00.000000000' '2019-04-09T18:00:00.000000000'
'2019-04-10T00:00:00.000000000' '2019-04-10T06:00:00.000000000'
'2019-04-10T12:00:00.000000000' '2019-04-10T18:00:00.000000000'
'2019-04-11T00:00:00.000000000']

那么,问题出在哪里?在你的nc上有dsloc.step.value,它的向量,然后你必须迭代向量

如何修复?只需查看步骤类型,立即尝试:

import pandas as pd
from random import sample
stations = pd.read_excel('KONYA_LONG_LAT_4digit.xlsx')
#station = stations.sample(n=1) # We can randomly choose a station
station=stations[stations['stname'] == 300 ] # We have chosen a station for consistency

import xarray as xr
#ds = xr.open_dataset('snow.nc')
ds = xr.open_dataset('adaptor.efas_mars.external-1615983508.657324-23066-19-648c63b0-a6b0-4568-8970-d0f966ff16a2.nc')

# extract data for selected point in netcdf file by LISFLOOD coordinates
dsloc = ds.sel(x=station.lat.values,y=station.long.values,method='nearest')

import numpy as np
base=dsloc.time.values
steps = dsloc.step.values
if type(steps) == np.timedelta64:
time=np.array([base+np.timedelta64(steps)])
else:
time=np.array([base+np.timedelta64(step) for step in steps])
print(time)

最新更新