xarray 多维插值到没有大矩阵的点

发布于 2025-01-13 02:45:22 字数 1015 浏览 0 评论 0原文

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

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

大数组

在此处输入图像描述

所需结果(如果未循环):

在此处输入图像描述

Is there a way to interpolate to specific points multi-dimensionally without creating an enormous array / looping?

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

Large array

enter image description here:

Desired result (if isn't looped):

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2025-01-20 02:45:22

当您想要使用多维点列表从多个维度中进行选择(而不是使用正交索引对数据进行子设置)时,您需要使用具有公共索引的 DataArray 从数据中进行选择:

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

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

When you want to select from multiple dimensions using a list of multidimensional points (rather than sub-setting the data using orthogonal indices), you want to select from the data using DataArrays with a common index:

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

See the docs on More Advanced Indexing for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文