6
6
import numpy as np
7
7
from packaging .version import Version
8
8
9
+ from xarray .core import pycompat
9
10
from xarray .core .computation import apply_ufunc
10
11
from xarray .core .options import _get_keep_attrs
11
12
from xarray .core .pdcompat import count_not_none
12
13
from xarray .core .types import T_DataWithCoords
13
-
14
- try :
15
- import numbagg
16
- from numbagg import move_exp_nanmean , move_exp_nansum
17
-
18
- _NUMBAGG_VERSION : Version | None = Version (numbagg .__version__ )
19
- except ImportError :
20
- _NUMBAGG_VERSION = None
14
+ from xarray .core .utils import module_available
21
15
22
16
23
17
def _get_alpha (
@@ -83,17 +77,17 @@ def __init__(
83
77
window_type : str = "span" ,
84
78
min_weight : float = 0.0 ,
85
79
):
86
- if _NUMBAGG_VERSION is None :
80
+ if not module_available ( "numbagg" ) :
87
81
raise ImportError (
88
82
"numbagg >= 0.2.1 is required for rolling_exp but currently numbagg is not installed"
89
83
)
90
- elif _NUMBAGG_VERSION < Version ("0.2.1" ):
84
+ elif pycompat . mod_version ( "numbagg" ) < Version ("0.2.1" ):
91
85
raise ImportError (
92
- f"numbagg >= 0.2.1 is required for rolling_exp but currently version { _NUMBAGG_VERSION } is installed"
86
+ f"numbagg >= 0.2.1 is required for rolling_exp but currently version { pycompat . mod_version ( 'numbagg' ) } is installed"
93
87
)
94
- elif _NUMBAGG_VERSION < Version ("0.3.1" ) and min_weight > 0 :
88
+ elif pycompat . mod_version ( "numbagg" ) < Version ("0.3.1" ) and min_weight > 0 :
95
89
raise ImportError (
96
- f"numbagg >= 0.3.1 is required for `min_weight > 0` within `.rolling_exp` but currently version { _NUMBAGG_VERSION } is installed"
90
+ f"numbagg >= 0.3.1 is required for `min_weight > 0` within `.rolling_exp` but currently version { pycompat . mod_version ( 'numbagg' ) } is installed"
97
91
)
98
92
99
93
self .obj : T_DataWithCoords = obj
@@ -127,13 +121,15 @@ def mean(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
127
121
Dimensions without coordinates: x
128
122
"""
129
123
124
+ import numbagg
125
+
130
126
if keep_attrs is None :
131
127
keep_attrs = _get_keep_attrs (default = True )
132
128
133
129
dim_order = self .obj .dims
134
130
135
131
return apply_ufunc (
136
- move_exp_nanmean ,
132
+ numbagg . move_exp_nanmean ,
137
133
self .obj ,
138
134
input_core_dims = [[self .dim ]],
139
135
kwargs = self .kwargs ,
@@ -163,13 +159,15 @@ def sum(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
163
159
Dimensions without coordinates: x
164
160
"""
165
161
162
+ import numbagg
163
+
166
164
if keep_attrs is None :
167
165
keep_attrs = _get_keep_attrs (default = True )
168
166
169
167
dim_order = self .obj .dims
170
168
171
169
return apply_ufunc (
172
- move_exp_nansum ,
170
+ numbagg . move_exp_nansum ,
173
171
self .obj ,
174
172
input_core_dims = [[self .dim ]],
175
173
kwargs = self .kwargs ,
@@ -194,10 +192,12 @@ def std(self) -> T_DataWithCoords:
194
192
Dimensions without coordinates: x
195
193
"""
196
194
197
- if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version ("0.4.0" ):
195
+ if pycompat . mod_version ( "numbagg" ) < Version ("0.4.0" ):
198
196
raise ImportError (
199
- f"numbagg >= 0.4.0 is required for rolling_exp().std(), currently { _NUMBAGG_VERSION } is installed"
197
+ f"numbagg >= 0.4.0 is required for rolling_exp().std(), currently { pycompat . mod_version ( 'numbagg' ) } is installed"
200
198
)
199
+ import numbagg
200
+
201
201
dim_order = self .obj .dims
202
202
203
203
return apply_ufunc (
@@ -225,12 +225,12 @@ def var(self) -> T_DataWithCoords:
225
225
array([ nan, 0. , 0.46153846, 0.18461538, 0.06446281])
226
226
Dimensions without coordinates: x
227
227
"""
228
-
229
- if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version ("0.4.0" ):
228
+ if pycompat .mod_version ("numbagg" ) < Version ("0.4.0" ):
230
229
raise ImportError (
231
- f"numbagg >= 0.4.0 is required for rolling_exp().var (), currently { _NUMBAGG_VERSION } is installed"
230
+ f"numbagg >= 0.4.0 is required for rolling_exp().std (), currently { pycompat . mod_version ( 'numbagg' ) } is installed"
232
231
)
233
232
dim_order = self .obj .dims
233
+ import numbagg
234
234
235
235
return apply_ufunc (
236
236
numbagg .move_exp_nanvar ,
@@ -258,11 +258,12 @@ def cov(self, other: T_DataWithCoords) -> T_DataWithCoords:
258
258
Dimensions without coordinates: x
259
259
"""
260
260
261
- if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version ("0.4.0" ):
261
+ if pycompat . mod_version ( "numbagg" ) < Version ("0.4.0" ):
262
262
raise ImportError (
263
- f"numbagg >= 0.4.0 is required for rolling_exp().cov (), currently { _NUMBAGG_VERSION } is installed"
263
+ f"numbagg >= 0.4.0 is required for rolling_exp().std (), currently { pycompat . mod_version ( 'numbagg' ) } is installed"
264
264
)
265
265
dim_order = self .obj .dims
266
+ import numbagg
266
267
267
268
return apply_ufunc (
268
269
numbagg .move_exp_nancov ,
@@ -291,11 +292,12 @@ def corr(self, other: T_DataWithCoords) -> T_DataWithCoords:
291
292
Dimensions without coordinates: x
292
293
"""
293
294
294
- if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version ("0.4.0" ):
295
+ if pycompat . mod_version ( "numbagg" ) < Version ("0.4.0" ):
295
296
raise ImportError (
296
- f"numbagg >= 0.4.0 is required for rolling_exp().cov (), currently { _NUMBAGG_VERSION } is installed"
297
+ f"numbagg >= 0.4.0 is required for rolling_exp().std (), currently { pycompat . mod_version ( 'numbagg' ) } is installed"
297
298
)
298
299
dim_order = self .obj .dims
300
+ import numbagg
299
301
300
302
return apply_ufunc (
301
303
numbagg .move_exp_nancorr ,
0 commit comments