python - 合并具有重叠坐标的多个 xarray 数据集

标签 python python-xarray

我正在尝试将具有重叠坐标的多个数据集合并为一个。当我将 compat= 设置为 'override' 时,仅保留第一个数据集的值,而结果数据集的其余部分设置为 nan。对于有冲突的单元格,我可以使用任何相交值。

看下面的例子

import numpy as np
import pandas as pd
import xarray as xr


temperature = np.random.randint(1,255,size=(9,10,10))
precipitation = np.random.randint(1,255,size=(9,10,10))
lon = np.linspace(10,100,10)
lat = np.linspace(10,100,10)
time_0 = pd.date_range("2014-09-06", periods=9, freq='2D')
time_1 = pd.date_range("2014-09-06", periods=9, freq='3D')

ds_0 = xr.Dataset(
       data_vars=dict(
           temperature=(('time', 'y', 'x'), temperature),
           precipitation=(('time', 'y', 'x'), precipitation)),
       coords=dict(
           x=lon,
           y=lat,
           time=time_0)
)
ds_1 = xr.Dataset(
       data_vars=dict(
           temperature=(('time', 'y', 'x'), temperature),
           precipitation=(('time', 'y', 'x'), precipitation)),
       coords=dict(
           x=lon + 90,
           y=lat,
           time=time_0)
)
ds_2 = xr.Dataset(
       data_vars=dict(
           temperature=(('time', 'y', 'x'), temperature),
           precipitation=(('time', 'y', 'x'), precipitation)),
       coords=dict(
           x=lon + 90,
           y=lat + 90,
           time=time_1)
)
ds_3 = xr.Dataset(
       data_vars=dict(
           temperature=(('time', 'y', 'x'), temperature),
           precipitation=(('time', 'y', 'x'), precipitation)),
       coords=dict(
           x=lon,
           y=lat + 90,
           time=time_1)
)

# Combine
ds = xr.merge([ds_0, ds_1, ds_2, ds_3], compat='override')
print(ds)

最佳答案

是的 - 当 compat='override'xr.merge 的预期和记录行为是使用第一个传递的数据对象的坐标。来自xr.merge docs :

compat ({"identical", "equals", "broadcast_equals", "no_conflicts", "override"}, optional) – String indicating how to compare variables of the same name for potential conflicts:

  • “broadcast_equals”: all values must be equal when variables are broadcast against each other to ensure common dimensions.

  • “equals”: all values and dimensions must be the same.

  • “identical”: all values, dimensions and attributes must be the same.

  • “no_conflicts”: only values which are not null in both datasets must be equal. The returned dataset then contains the combination of all non-null values.

  • “override”: skip comparing and pick variable from first dataset

所以我认为您可能正在寻找 compat='no_conflicts'

关于python - 合并具有重叠坐标的多个 xarray 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71487497/

相关文章:

python - Scrapy回调函数

python-3.x - 提取几何(形状)内的数据

python - 将 3 维 xr.DataArray (Xarray) 展平/拆解/折叠成沿轴的二维?

python - 使用 xarray 导入时,netCDF 文件在 python 中没有变量

netcdf 文件的 axarray 中的 IO 后端错误

python - 使用python 3.6解码utf-8字符串

python - 如何按周一、周二对时间序列数据进行分组? Pandas

python - 如何用sympy求解矩阵方程?

python - 在我使用 PyInstaller 将 PY 转换为 EXE 后,它抛出错误

python - 如何按列值 reshape Pandas 数据框?