44
44
# methods which remove an axis
45
45
REDUCE_METHODS = ['all' , 'any' ]
46
46
NAN_REDUCE_METHODS = ['argmax' , 'argmin' , 'max' , 'min' , 'mean' , 'prod' , 'sum' ,
47
- 'std' , 'var' , 'median' ]
47
+ 'std' , 'var' , 'median' , 'cumsum' , 'cumprod' ]
48
48
BOTTLENECK_ROLLING_METHODS = {'move_sum' : 'sum' , 'move_mean' : 'mean' ,
49
49
'move_std' : 'std' , 'move_min' : 'min' ,
50
50
'move_max' : 'max' }
51
- # TODO: wrap cumprod/cumsum, take, dot, sort
51
+ # TODO: wrap take, dot, sort
52
52
53
53
54
54
def _dask_or_eager_func (name , eager_module = np , list_of_args = False ,
@@ -274,7 +274,9 @@ def _ignore_warnings_if(condition):
274
274
yield
275
275
276
276
277
- def _create_nan_agg_method (name , numeric_only = False , coerce_strings = False ):
277
+ def _create_nan_agg_method (name , numeric_only = False , np_compat = False ,
278
+ no_bottleneck = False , coerce_strings = False ,
279
+ keep_dims = False ):
278
280
def f (values , axis = None , skipna = None , ** kwargs ):
279
281
# ignore keyword args inserted by np.mean and other numpy aggregators
280
282
# automatically:
@@ -292,14 +294,17 @@ def f(values, axis=None, skipna=None, **kwargs):
292
294
'skipna=True not yet implemented for %s with dtype %s'
293
295
% (name , values .dtype ))
294
296
nanname = 'nan' + name
295
- if isinstance (axis , tuple ) or not values .dtype .isnative :
297
+ if isinstance (axis , tuple ) or not values .dtype .isnative or no_bottleneck :
296
298
# bottleneck can't handle multiple axis arguments or non-native
297
299
# endianness
298
- eager_module = np
300
+ if np_compat :
301
+ eager_module = npcompat
302
+ else :
303
+ eager_module = np
299
304
else :
300
305
eager_module = bn
301
306
func = _dask_or_eager_func (nanname , eager_module )
302
- using_numpy_nan_func = eager_module is np
307
+ using_numpy_nan_func = eager_module is np or eager_module is npcompat
303
308
else :
304
309
func = _dask_or_eager_func (name )
305
310
using_numpy_nan_func = False
@@ -312,10 +317,12 @@ def f(values, axis=None, skipna=None, **kwargs):
312
317
else :
313
318
assert using_numpy_nan_func
314
319
msg = ('%s is not available with skipna=False with the '
315
- 'installed version of numpy; upgrade to numpy 1.9 '
320
+ 'installed version of numpy; upgrade to numpy 1.12 '
316
321
'or newer to use skipna=True or skipna=None' % name )
317
322
raise NotImplementedError (msg )
318
323
f .numeric_only = numeric_only
324
+ f .keep_dims = keep_dims
325
+ f .__name__ = name
319
326
return f
320
327
321
328
@@ -328,28 +335,18 @@ def f(values, axis=None, skipna=None, **kwargs):
328
335
std = _create_nan_agg_method ('std' , numeric_only = True )
329
336
var = _create_nan_agg_method ('var' , numeric_only = True )
330
337
median = _create_nan_agg_method ('median' , numeric_only = True )
331
-
338
+ prod = _create_nan_agg_method ('prod' , numeric_only = True , np_compat = True ,
339
+ no_bottleneck = True )
340
+ cumprod = _create_nan_agg_method ('cumprod' , numeric_only = True , np_compat = True ,
341
+ no_bottleneck = True , keep_dims = True )
342
+ cumsum = _create_nan_agg_method ('cumsum' , numeric_only = True , np_compat = True ,
343
+ no_bottleneck = True , keep_dims = True )
332
344
333
345
_fail_on_dask_array_input_skipna = partial (
334
346
_fail_on_dask_array_input ,
335
347
msg = '%r with skipna=True is not yet implemented on dask arrays' )
336
348
337
349
338
- _prod = _dask_or_eager_func ('prod' )
339
-
340
-
341
- def prod (values , axis = None , skipna = None , ** kwargs ):
342
- if skipna or (skipna is None and values .dtype .kind == 'f' ):
343
- if values .dtype .kind not in ['i' , 'f' ]:
344
- raise NotImplementedError (
345
- 'skipna=True not yet implemented for prod with dtype %s'
346
- % values .dtype )
347
- _fail_on_dask_array_input_skipna (values )
348
- return npcompat .nanprod (values , axis = axis , ** kwargs )
349
- return _prod (values , axis = axis , ** kwargs )
350
- prod .numeric_only = True
351
-
352
-
353
350
def first (values , axis , skipna = None ):
354
351
"""Return the first non-NA elements in this array along the given axis
355
352
"""
0 commit comments