根据特定月份选择xarray/pandas索引 [英] Select xarray/pandas index based on specific months

查看:377
本文介绍了根据特定月份选择xarray/pandas索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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的内置andor不适用于矢量.另外,您还需要括号,因为按位运算符的优先级高于比较.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆