根据特定月份选择xarray/pandas索引 [英] Select xarray/pandas index based on specific months
问题描述
我有一个xarray DataArray,我想为整个时间序列选择4月,5月,6月(类似于time.season =='JJA').
I have an xarray DataArray that I want to select the months April, May, June (similar to time.season=='JJA') for an entire time series.
其结构类似于:
<xarray.DataArray 't2m' (time: 492, latitude: 81, longitude: 141)>
我以前通过以下方式选择JJA:
I have been previously selecting JJA by:
seasonal_data =temp_data.sel(time=temp_data['time.season']=='JJA')
我想做同样的事情,但是用"AMJ"月份代替.我可以添加可能会丢失的任何详细信息.
I would like to do the same thing but with the months 'AMJ' instead. I can add any details that I might be missing.
谢谢
推荐答案
选择自定义月份的最简单方法是使用布尔值掩码,例如
The easiest way to select custom months is to use boolean masks, e.g.,
def is_amj(month):
return (month >= 4) & (month <= 6)
seasonal_data = temp_data.sel(time=is_amj(temp_data['time.month']))
请注意,您需要使用像&
或|
这样的按位运算符,因为Python的内置and
和or
不适用于矢量.另外,您还需要括号,因为按位运算符的优先级高于比较.
Note that you need to use the bitwise operators like &
or |
because Python's built-ins and
and or
don't work on vectors. Also, you need the parentheses because bitwise operators have higher precedence than comparisons.
这篇关于根据特定月份选择xarray/pandas索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!