Python 扩展xarray数据集

Python 扩展xarray数据集,python,python-xarray,Python,Python Xarray,我正在尝试向xarray教程数据集中添加霜冻天数计数 airtemps = xr.tutorial.load_dataset('air_temperature') # The set spans more than a year, let's take only one airtemps = airtemps.sel(time=slice('2013-01-01', '2013-12-31')) airtemps['air'] = airtemps.air - 273.15 数据的分辨率非常高

我正在尝试向xarray教程数据集中添加霜冻天数计数

airtemps = xr.tutorial.load_dataset('air_temperature')
# The set spans more than a year, let's take only one
airtemps = airtemps.sel(time=slice('2013-01-01', '2013-12-31'))
airtemps['air'] = airtemps.air - 273.15
数据的分辨率非常高,所以为了便于处理,我对它们进行了重新采样 air\u day=airtemps.resample('1D','time','how='mean') 空气月=空气日。重新采样('1M','time','how='mean')

因此,我希望在
air\u month
中有一个额外的变量,在所有三个维度上,平均值低于零的天数计数

我的尝试就是这样

air_month['frost'] = sum(air_day.air < 0)
air_月['frost']=总和(air_日.air<0)
然而,通过
sum()
ing,我在这里失去了时间维度。我被困在这里了,仍然没有把xarray的概念包在我的头上

谢谢你的帮助

你就快到了! 而且你的groupby非常相似(考虑到xarray的重采样还不是groupby,在某些方面更好)

[24]中的
:(air\u day.air<0)。重新采样('M',dim='time',how='sum'))
出[24]:
数组([[31,31,31,…,31,31,31],
[31, 31, 31, ..., 31, 31, 31],
[31, 31, 31, ..., 31, 31, 31],
...,
[ 0,  0,  0, ...,  0,  0,  0],
[ 0,  0,  0, ...,  0,  0,  0],
[ 0,  0,  0, ...,  0,  0,  0]]])
协调:
*lat浮动3275.072.570.067.565.062.560.057.555.052.5。。。
*lon(lon)float32200.0202.5205.0207.5210.0212.5215.0217.5。。。
*时间日期时间64[ns]2013-01-31 2013-02-28 2013-03-31。。。
你就快到了! 而且你的groupby非常相似(考虑到xarray的重采样还不是groupby,在某些方面更好)

[24]中的
:(air\u day.air<0)。重新采样('M',dim='time',how='sum'))
出[24]:
数组([[31,31,31,…,31,31,31],
[31, 31, 31, ..., 31, 31, 31],
[31, 31, 31, ..., 31, 31, 31],
...,
[ 0,  0,  0, ...,  0,  0,  0],
[ 0,  0,  0, ...,  0,  0,  0],
[ 0,  0,  0, ...,  0,  0,  0]]])
协调:
*lat浮动3275.072.570.067.565.062.560.057.555.052.5。。。
*lon(lon)float32200.0202.5205.0207.5210.0212.5215.0217.5。。。
*时间日期时间64[ns]2013-01-31 2013-02-28 2013-03-31。。。

不确定这是否是一种方法,但似乎有效:
air\u day['frost']=air\u day.air<0
air\u day.frost.groupby('time.month').sum('time').plot(col='month',col u wrap=3)
如何及时向前扩展数据?因此,如果您想使用日期范围
pd.data\u range('2013-01-01','2013-12-31')来复制
airtemps.sel(time=0)
及时转发
pd.data\u range('2013-01-01','2013-12-31'))
?不确定这是否是一种方法,但似乎有效:
air\u day['frost']=air\u day.air<0
air\u day.frost.groupby('time.month').sum('time').time')。绘图(col month',col wrap=3)
您将如何及时向前扩展数据?因此,如果您想使用日期范围
pd.data\u范围('2013-01-01','2013-12-31')复制
airtemps.sel(time=0)
及时转发
?非常好,谢谢!这就是我想象中的样子!太好了,谢谢!这就是我想象中的样子!
In [24]: (air_day.air < 0).resample('M', dim='time', how='sum')
Out[24]:
<xarray.DataArray 'air' (time: 12, lat: 25, lon: 53)>
array([[[31, 31, 31, ..., 31, 31, 31],
        [31, 31, 31, ..., 31, 31, 31],
        [31, 31, 31, ..., 31, 31, 31],
        ...,
        [ 0,  0,  0, ...,  0,  0,  0],
        [ 0,  0,  0, ...,  0,  0,  0],
        [ 0,  0,  0, ...,  0,  0,  0]]])
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 62.5 60.0 57.5 55.0 52.5 ...
  * lon      (lon) float32 200.0 202.5 205.0 207.5 210.0 212.5 215.0 217.5 ...
  * time     (time) datetime64[ns] 2013-01-31 2013-02-28 2013-03-31 ...