簡體   English   中英

時間片 Python Xarray Dataarray

[英]Time Slice Python Xarray Dataarray

我需要知道我是否對我擁有的 1991 年 1 月至 2021 年 12 月的 xarray 數據進行時間切片。 坐標如下所示:

Coordinates:
 * time       (time) datetime64[ns] 1991-01-31 1991-02-28 ... 2021-12-31
number     int32 0
step       timedelta64[ns] 00:00:00
surface    float64 0.0
 * latitude   (latitude) float64 58.0 57.75 57.5 57.25 ... 23.5 23.25 23.0
 * longitude  (longitude) float64 -130.0 -129.8 -129.5 ... -63.5 -63.25 -63.0

我用來切片數據數組(resultm)的代碼行如下所示 -

month_curr = resultm.sel(time=slice('2021-12','2021-12')).groupby('time.month').mean(dim='time')

而且,我的目標是分割或提取所有 2021 年 12 月的數據——這應該是月度值。 看起來數據可能是每日形式,但下載類型是“monthly_averaged_reanalysis”ERA5 數據。

謝謝,

您可以使用日期時間訪問器.dt select 相關數據,其中您需要使用numpy.logical_and組合dt.monthdt.year並生成對應於所需索引的 Z84E2C64F38F28BA3EA5C905AB5 索引。

對於您的示例,要生成 2021 年 12 月的月平均值,您可以執行以下操作:

import numpy as np

month_curr = resultm.sel(
    time=np.logical_and(
        resultm.time.dt.year == 2021, resultm.time.dt.month == 12
    )
)
month_curr = month_curr.mean("time")

這是一個玩具示例(使用 2013 年):

import xarray as xr
import numpy as np

x = xr.tutorial.load_dataset("air_temperature")

xs = x.sel(
    time=np.logical_and(x.time.dt.month == 12, x.time.dt.year == 2013)).mean("time")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM