簡體   English   中英

在 xarray 中是否有另一種替代 DataArray.sel(time = slice(x, y)) 的方法?

[英]Is there another alternative to DataArray.sel(time = slice(x, y)) in xarray?

出於某種原因,DataArray.sel(time = slice(x, y)) 在 1 月到 6 月期間對我來說沒有任何問題,其中 x 和 y 都等於從 1 到 6 的值六月。 但是,這種方法不適用於 7 月到 12 月。 我已經檢查了輸入數據,它是一個 .netCDF4 文件並且沒有損壞。 因此,我正在尋找一種替代方法來代替 xarray 中的 DataArray.sel(time = slice(x, y)) 來提取 7 月至 12 月的數據。

代碼如下:

import xarray as xr
td = xr.open_dataset(r'C:\Users\abc\Desktop\misc\netcdf_to_geotiff\ECLIPSEv5_monthly_patterns.nc')
td_agr = td.agr
td_agrtime = td_agr.sel(time = slice('1', '1'))

這給出了 output:

In [7]: td_agrtime
Out[7]: 
<xarray.DataArray 'agr' (time: 1, lat: 360, lon: 720)>
[259200 values with dtype=float64]
Coordinates:
* lat      (lat) float64 -89.75 -89.25 -88.75 -88.25 ... 88.75 89.25 
89.75
* lon      (lon) float64 -179.8 -179.2 -178.8 -178.2 ... 178.8 179.2 
179.8
* time     (time) int32 1
Attributes:
long_name:  Monthly weights - Agriculture (animals, rice, soil)
sector:     Agriculture (animals, rice, soil)

如果把代碼中的1改成7如下:

td_agrtime = td_agr.sel(time = slice('7', '7')

output 是:

In [7]: td_agrtime
Out[9]: 
<xarray.DataArray 'agr' (time: 6, lat: 360, lon: 720)>
[1555200 values with dtype=float64]
Coordinates:
* lat      (lat) float64 -89.75 -89.25 -88.75 -88.25 ... 88.75 89.25 
89.75
* lon      (lon) float64 -179.8 -179.2 -178.8 -178.2 ... 178.8 179.2 
179.8
* time     (time) int32 7 8 9 10 11 12
Attributes:
long_name:  Monthly weights - Agriculture (animals, rice, soil)
sector:     Agriculture (animals, rice, soil)

感謝羅伯特戴維的評論。 答案是使用.isel()而不是.sel()

暫無
暫無

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

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