xarray多维插值到没有大矩阵的点
xarray multi-dimensional interpolate to point without large matrix
有没有办法在不创建巨大数组/循环的情况下对特定点进行多维插值?
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()])
大数组
:
期望的结果(如果没有循环):
当您想要使用多维点列表从多个维度 select 时(而不是使用正交索引对数据进行子设置),您想要使用 DataArrays 从数据中 select常用索引:
# 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。
有没有办法在不创建巨大数组/循环的情况下对特定点进行多维插值?
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()])
大数组
期望的结果(如果没有循环):
当您想要使用多维点列表从多个维度 select 时(而不是使用正交索引对数据进行子设置),您想要使用 DataArrays 从数据中 select常用索引:
# 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。