【问题标题】:xarray multi-dimensional interpolate to point without large matrixxarray多维插值到没有大矩阵的点
【发布时间】:2019-05-15 06:44:13
【问题描述】:

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

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

大数组

:

想要的结果(如果没有循环):

【问题讨论】:

    标签: interpolation python-xarray


    【解决方案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 了解更多信息。

    【讨论】:

    • 哇!谢谢,太神奇了。一条长线pdf[['air', 'airx2']] = xds.interp(**{col: pdf[col].to_xarray() for col in df.columns}).to_dataframe()[['air', 'airx2']]
    • 其实更短:pdf[['air', 'airx2']] = xds.interp(pdf.to_xarray()).to_dataframe()[['air', 'airx2']]
    • 哇...我以前没见过这种语法!酷!
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 2023-03-23
    • 2011-07-16
    • 1970-01-01
    • 2018-03-28
    • 2014-09-06
    • 2014-02-09
    • 1970-01-01
    相关资源
    最近更新 更多