首页 > 解决方案 > xarray多维插值到没有大矩阵的点

问题描述

有没有办法在不创建巨大数组/循环的情况下多维插入特定点?

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()])

大阵列

在此处输入图像描述

期望的结果(如果没有循环):

在此处输入图像描述

标签: interpolationpython-xarray

解决方案


当您想使用多维点列表从多个维度中进行选择(而不是使用正交索引对数据进行子设置)时,您希望使用具有公共索引的 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()

有关更多信息,请参阅有关更高级索引的文档


推荐阅读