python - xarray - 从另一个 DataArray 的时间标签中选择/索引 DataArray

标签 python pandas numpy python-xarray

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

除了纬度经度之外,它们都有一个时间维度来表示每日数据。 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 的时间 维度是B 的时间 维度的子集。我想使用 A 中的所有 time 标签从 B 中选择数据。由于 A 中的时间不连续,我认为 slice 不合适。所以我尝试使用sel

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

我收到错误:KeyError:“并非在索引“时间”中找到所有值”

最佳答案

A_new = A.where(A.time.isin(B.time), drop=True)

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

关于python - xarray - 从另一个 DataArray 的时间标签中选择/索引 DataArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57781130/

相关文章:

python - Pandas df.to_records() 返回一维 numpy 数组

python - 从 'partial' 二项分布进行高效采样

python - 当最后一层不是 `softmax` 时(例如集成模型),如何应用 Cleverhans 攻击?

python - 设计模式在 Web 开发中有多重要?

python - 在python中查找指定范围内的图像像素的坐标

python - Pandas 获取行组合和组

python - Pandas DataFrame,如何删除总和为 0 的所有列和行

python - Elasticsearch DSL 过滤器

python - 获取AWS S3中所有存储桶的名称和数量

python - 当我传递 skip_footer arg 时,Pandas read_csv 忽略列数据类型