python - How to merge xArray datasets with conflicting coordinates -
let's have 2 data sets, each containing different variable of interest , incomplete (but not conflicting) indices:
in [1]: import xarray xr, numpy np in [2]: ages = xr.dataset( {'ages': (['kid_ids'], np.random.rand((3))*20)}, coords={'kid_names':(['kid_ids'], ['carl','kathy','gail']), 'kid_ids': [10,14,16]}) in [3]: heights = xr.dataset( {'heights': (['kid_ids'], np.random.rand((3))*160)}, coords={'kid_names':(['kid_ids'], ['carl','keith','gail']), 'kid_ids': [10,13,16]})
this creates 2 data sets seem should merge well:
in [4]: ages out[4]: <xarray.dataset> dimensions: (kid_ids: 3) coordinates: * kid_ids (kid_ids) int32 10 14 16 kid_names (kid_ids) <u5 'carl' 'kathy' 'gail' data variables: ages (kid_ids) float64 13.28 1.955 4.327 in [5]: heights out[5]: <xarray.dataset> dimensions: (kid_ids: 3) coordinates: * kid_ids (kid_ids) int32 10 13 16 kid_names (kid_ids) <u5 'carl' 'keith' 'gail' data variables: heights (kid_ids) float64 115.0 38.2 31.65
but don't - attempting ages.merge(heights)
causes valueerror
:
valueerror: conflicting value variable kid_names: first value: <xarray.variable (kid_ids: 4)> array(['carl', nan, 'kathy', 'gail'], dtype=object) second value: <xarray.variable (kid_ids: 4)> array(['carl', 'keith', nan, 'gail'], dtype=object)
dropping coordinate kid_names
solves problem:
in [7]: ages.reset_coords('kid_names', drop=true).merge( heights.reset_coords('kid_names', drop=true)) out[7]: <xarray.dataset> dimensions: (kid_ids: 4) coordinates: * kid_ids (kid_ids) int64 10 13 14 16 data variables: ages (kid_ids) float64 0.4473 nan 6.45 6.787 heights (kid_ids) float64 78.42 78.43 nan 113.4
it seems though coordinates being handled dataarrays
, in non-identical values raise error. shouldn't handled more base coordinates, e.g. extended superset of 2 indices? or there operation should doing?
i'm on python 3.5 using xarray 0.7.2 , numpy 1.10.4
this isn't easy achieve in xarray, should be!
in fact, think should safe merge non-conflicting values under circumstances (unless user requests higher scrutiny).
i opened github issue track this: https://github.com/pydata/xarray/issues/835
Comments
Post a Comment