【发布时间】:2018-06-21 05:07:09
【问题描述】:
我成功实现了来自Select xarray/pandas index based on specific months 的shoyer 回答。
def is_amj(month):
return (month >= 4) & (month <= 6)
seasonal_data = temp_data.sel(time=is_amj(temp_data['time.month']))
不幸的是,我需要更灵活地选择月份(例如 1 月和 12 月,或 2 月至 11 月,或 1 月、3 月、5 月……)。我想使用月份列表。
我尝试修改代码如下
sel_months = [1,3,5] #in the case of january,march and may
def to_keep(month):
return (month in sel_months)
seasonal_data = temp_data.sel(time=to_keep(temp_data['time.month']))
但我收到以下消息
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
并且没有找到实施 a.any() 建议的正确方法。
欢迎任何帮助。
【问题讨论】:
-
试试怎么样:
np.isin(month, sel_months)
标签: python pandas numpy python-xarray