interpolation - 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()])
大阵列
期望的结果(如果没有循环):
解决方案
当您想使用多维点列表从多个维度中进行选择(而不是使用正交索引对数据进行子设置)时,您希望使用具有公共索引的 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()
有关更多信息,请参阅有关更高级索引的文档。
推荐阅读
- php - Laravel 中的复选框
- python - 无法在 ConversationHandler 中使用 python-telegram-bot 调用函数
- javascript - 原型继承如何影响 JavaScript 中的方法覆盖?
- rest - 如何从 R 中的 UCSC 基因组 API 请求中检索数据?
- android - 如何在flutter andriod中实现文件观察器?
- c# - 在数据库中添加了额外的列,但代码看不到它们
- php - Laravel Auth::user() 返回 null 除了登录功能
- c# - 使用规范模式按相关属性过滤
- python - 从 QWebEngineView 获取 PDF 文件的链接
- istio - ISTIO 1.9.5 - 如何在 istio 发生本地配置更改时发送通知