xarray - select/index 来自另一个 DataArray 的时间标签的 DataArray

xarray - select/index DataArray from the time labels from another DataArray

我有两个 DataArray 对象,名为“A”和“B”。

除了LatitudeLongitude,它们都有一个time维度表示每日数据。 A 的时间坐标小于 B

A的时间维度:

<xarray.DataArray 'time' (time: 1422)>
array(['2015-03-30T00:00:00.000000000', '2015-06-14T00:00:00.000000000',
       '2015-06-16T00:00:00.000000000', ..., '2019-08-31T00:00:00.000000000',
       '2019-09-01T00:00:00.000000000', '2019-09-02T00:00:00.000000000'],
      dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 2015-03-30 2015-06-14 ... 2019-09-02

B的时间维度:

<xarray.DataArray 'time' (time: 16802)>
array(['1972-01-01T00:00:00.000000000', '1972-01-02T00:00:00.000000000',
       '1972-01-03T00:00:00.000000000', ..., '2017-12-29T00:00:00.000000000',
       '2017-12-30T00:00:00.000000000', '2017-12-31T00:00:00.000000000'],
      dtype='datetime64[ns]')
Coordinates:
  * time     (time) datetime64[ns] 1972-01-01 1972-01-02 ... 2017-12-31

显然,A 的 time 维度是 B 的 time 维度的子集。我想使用来自 A 的所有 time 标签从 B 获取 select 数据。由于 A 中的时间不连续,我认为 slice 不合适。所以我尝试使用 sel.

B_sel = B.sel(time=A.time)

我收到一个错误:KeyError: "not all values found in index 'time'"

Obviously, the A's time dimension is a subset of B's time dimension.

I received an error: KeyError: "not all values found in index 'time'"

该错误消息本身就暗示了陈述一中的假设是错误的。此外,如果您仔细查看时间值,A 的值会持续到 2019 年,而 B 的值会在 2017 年结束。

所以,有两种方法可以解决这个问题:

  1. 如果您确定 A 具有 B 中截至 2017 年的所有值,那么

    sel_dates = A.time.values[A.time.dt.year < 2017]
    B_sel = B.sel(time=sel_dates)
    
  2. 如果您不确定 A 中的值是否连续,因为某处有一些意外值,那么您可以使用 np.isin() 执行逐元素检查,这是速度优化 numpy 函数

    sel_dates = A.time.values[np.isin(A.time.values, B.time.values)]
    
     ## example ##
     ## dates1 is an array of daily dates of 1 month
     dates1 = np.arange('2005-02', '2005-03', dtype='datetime64[D]')
     dates2 = np.array(['2005-02-03', '2002-02-05', '2000-01-05'], dtype='datetime64')
     # checking for dates2 which are a part of dates 1
     print(np.isin(dates2, dates1))
     >>array([ True, False, False])
    
A_new = A.where(A.time.isin(B.time), drop=True)

http://xarray.pydata.org/en/stable/user-guide/indexing.html