numpy 如何在xarray中执行类似于pandas的合并?

f45qwnt8  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(112)

我有一个长的框架与一个单一的重复维度(国家)和默认索引和一个二维框架共享相同的维度(国家),并有一个额外的(时间)
我想左合并xr.DataSet中的两个对象,以便根据匹配的国家/地区,左对象的每个默认索引都与右对象的1个向量(随时间变化)相关联
当我使用xarray执行合并时,我从左边的对象中选择一个特定的默认索引,我获得了第二个对象的所有可能组合,而不考虑相应的国家。示例显示了使用pandas的预期结果:

df1 = pd.DataFrame({
    'country': 4*['UK','US','FR','DE'],
    'var1': 10*np.arange(16),
}).reset_index(names='N')

ds1 = df1.set_index(['N','country']).to_xarray()

df2 = pd.DataFrame({
    'country': 4*['UK'] + 4*['US'] + 4*['FR'] + 4*['DE'],
    'time': 4*list(pd.timedelta_range(
        start='30D',
        periods=4,
        freq='30D'
    )),
    'var2': 20*np.arange(16),    
})

ds2 = df2.set_index(['country','time']).to_xarray()

# 1. xarray merge
ds3 = ds1.merge(ds2, join='left')
# Selecting specific original index
# returns all possible combinations without recognising corresponding country
display(ds3.sel(N=1).var2)

# 2. pandas merge
df3 = df1.set_index(['N','country']).merge(
    df2.pivot(index='country', columns='time'),
    left_index=True,
    right_index=True
)
# Selecting specific original index
# returns only vector for corresponding country
df3.xs(1, level='N')

字符串

yb3bgrhw

yb3bgrhw1#

问题似乎出在xarray处理合并的方式上,它没有保留'N'和'country'之间的关系。你应该先合并pandas中的DataFrames,然后将结果转换为xarray数据集。

merged_df = pd.merge(df1, df2, on='country')
merged_ds = merged_df.set_index(['N', 'country', 'time']).to_xarray()

字符串
来源:我的文章https://ioflood.com/blog/how-to-use-pandas-merge-with-dataframe-objects/

相关问题