【问题标题】:How to drop unmatching time series from two xarray time-series dataset如何从两个 xarray 时间序列数据集中删除不匹配的时间序列
【发布时间】:2021-04-12 20:13:59
【问题描述】:

我有两个具有匹配和不匹配时间序列的 xarray 数据集。我想从数据集 2 中删除与数据集 1 的时间序列不匹配的时间序列。

ds1
    <xarray.Dataset>
    Dimensions:      (time: 149, x: 311, y: 266)
    Coordinates:
      * y            (y) float64 -3.256e+06 -3.256e+06 ... -3.263e+06 -3.263e+06
        spatial_ref  int32 3577
      * time         (time) datetime64[ns] 2016-01-01T00:09:15.704000 ... 2020-12...
      * x            (x) float64 1.913e+06 1.913e+06 1.913e+06 ... 1.92e+06 1.92e+06
    Data variables:
        FMCOB          (time, y, x) float64 78.63 48.68 85.0 ... 42.16 91.27 52.36
        Forest       (x, y) int64 0 0 0 3 3 3 3 3 0 0 0 0 ... 0 0 3 3 3 3 3 3 3 3 3
        Grass        (x, y) int64 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0
        Shrub        (x, y) int64 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0
    Attributes:
        crs:           EPSG:3577
        grid_mapping:  spatial_ref
        units:         % dry matter

ds2
<xarray.Dataset>
    Dimensions:      (time: 155, x: 76, y: 47)
    Coordinates:
      * y            (y) float64 -3.257e+06 -3.257e+06 ... -3.258e+06 -3.258e+06
        spatial_ref  int32 3577
      * time         (time) datetime64[ns] 2016-01-01T00:09:15.704000 ... 2020-12...
      * x            (x) float64 1.919e+06 1.919e+06 ... 1.921e+06 1.921e+06
    Data variables:
        FMCOB          (time, y, x) float64 81.67 87.5 74.4 95.0 ... nan 58.39 85.96
        Forest       (x, y) int64 0 0 0 0 0 0 3 3 3 3 3 3 ... 0 0 0 0 0 0 0 0 0 0 0
        Grass        (x, y) int64 0 0 1 1 1 1 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0
        Shrub        (x, y) int64 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 2 0 0 0 0 2
    Attributes:
        crs:           EPSG:3577
        grid_mapping:  spatial_ref
        units:         % dry matter

我的尝试如下:

for i in ds1.time:
    for k in ds2.time:
        if k!=i:
            
            ds2.drop_sel(time = np.datetime64(k))
            

但这会引发以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-226-70fe5e9f97a4> in <module>
      4     for k in ds2.time:
      5         if k!=i:
----> 6             ds2.drop_sel(time = np.datetime64(k))

ValueError: Could not convert object to NumPy datetime   

【问题讨论】:

  • 试试ds2.sel(time=ds1.time)
  • @Val,感谢您的帮助。这返回错误:KeyError: "not all values found in index 'time'".
  • 这个错误来自数据集的时间不匹配

标签: python-xarray


【解决方案1】:

如果您想从ds2 中选择所有时间片,这些时间片也存在于ds1 中,您可以这样做

time_ix = np.isin(ds2.time, ds1.time)
ds2_sel = ds2.sel(time=time_ix)

其中time_ix 是一个简单的布尔数组,True 代表ds2.time 中的每个元素,ds1.time 中也出现。

【讨论】:

  • 太棒了!非常感谢@Val!那么我可以将 ds1 和 ds2 与 ds = ds1.merge(ds2, compat='override') 合并吗?数据集变量有不同的值,所以我想根据时间合并两个数据集的变量。
  • 我想你可以......但是看看文档以了解合并会发生什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多