有没有一种方法可以在不创建大量数组/循环的情况下多维地插值到特定点?
import xarray as xr
import pandas as pd
xds = xr.tutorial.open_dataset('air_temperature')
xds['airx2'] = xds['air'] * 2
pdf = pd.DataFrame(dict(lat=[45, 60, 75], lon=[225, 320, 315],
time=pd.to_datetime(['2013-01-10', '2013-01-12', '2013-01-15'])))
# this seems to be very fast, but creates a large 3x3x3 array
# not ideal if I have 800 rows which will make a final array of 800x800x800
xds.interp(**pdf)
# this doesn't create a 800x800x800 array
# if there's 800 rows in the dataframe, but not vectorized
pd.concat([xds.interp(**row).to_array().to_dataframe('kelvin')
for i, row in pdf.iterrows()])
大数组
所需结果(如果未循环显示):
答案 0 :(得分:1)
如果要使用多维点列表从多个维度中进行选择(而不是使用正交索引对数据进行子设置),则要使用具有公共索引的DataArrays从数据中进行选择:
# create three indexer DataArrays with the DataFrame's index as their coordinate
lat_idx = pdf.lat.to_xarray()
lon_idx = pdf.lon.to_xarray()
time_idx = pdf.time.to_xarray()
# interpolate to these *points* at the lat/lon/time positions given
interped = xds.interp(lat=lat_idx, lon=lon_idx, time=time_idx)
# this can be dumped into pandas:
interped_df = interped.to_dataframe()
有关更多信息,请参见docs on More Advanced Indexing。