@@ -2693,9 +2693,8 @@ def _aggregate(self, op, args, kwargs=None, keepaxes=False, by_agg=False, commut
2693
2693
res = func (op , axes , keepaxes = keepaxes , out = out , ** extra_kwargs )
2694
2694
return res
2695
2695
2696
- # op=sum does not parse correctly
2697
2696
def with_total (self , * args , ** kwargs ):
2698
- """with_total(*args, op=' sum' , label='total', **kwargs)
2697
+ r """with_total(*args, op=sum, label='total', **kwargs)
2699
2698
2700
2699
Add aggregated values (sum by default) along each axis.
2701
2700
A user defined label can be given to specified the computed values.
@@ -2706,7 +2705,8 @@ def with_total(self, *args, **kwargs):
2706
2705
Axes or groups along which to compute the aggregates. Passed groups should be named.
2707
2706
Defaults to aggregate over the whole array.
2708
2707
op : aggregate function, optional
2709
- Defaults to `sum`.
2708
+ Available aggregate functions are: `sum`, `prod`, `min`, `max`, `mean`, `ptp`, `var`, `std`,
2709
+ `median` and `percentile`. Defaults to `sum`.
2710
2710
label : scalar value, optional
2711
2711
Label to use for the total. Applies only to aggregated axes, not groups. Defaults to "total".
2712
2712
**kwargs : int or str or Group or any combination of those, optional
@@ -2718,30 +2718,43 @@ def with_total(self, *args, **kwargs):
2718
2718
2719
2719
Examples
2720
2720
--------
2721
- >>> arr = ndtest((3, 3) )
2721
+ >>> arr = ndtest("gender=M,F;time=2013..2016" )
2722
2722
>>> arr
2723
- a\\ b b0 b1 b2
2724
- a0 0 1 2
2725
- a1 3 4 5
2726
- a2 6 7 8
2723
+ gender\time 2013 2014 2015 2016
2724
+ M 0 1 2 3
2725
+ F 4 5 6 7
2727
2726
>>> arr.with_total()
2728
- a\\ b b0 b1 b2 total
2729
- a0 0 1 2 3
2730
- a1 3 4 5 12
2731
- a2 6 7 8 21
2732
- total 9 12 15 36
2733
- >>> arr.with_total('a', 'b0,b1 >> total_01')
2734
- a\\ b b0 b1 b2 total_01
2735
- a0 0 1 2 1
2736
- a1 3 4 5 7
2737
- a2 6 7 8 13
2738
- total 9 12 15 21
2739
- >>> arr.with_total(op=prod, label='product')
2740
- a\\ b b0 b1 b2 product
2741
- a0 0 1 2 0
2742
- a1 3 4 5 60
2743
- a2 6 7 8 336
2744
- product 0 28 80 0
2727
+ gender\time 2013 2014 2015 2016 total
2728
+ M 0 1 2 3 6
2729
+ F 4 5 6 7 22
2730
+ total 4 6 8 10 28
2731
+
2732
+ Using another function and label
2733
+
2734
+ >>> arr.with_total(op=mean, label='mean')
2735
+ gender\time 2013 2014 2015 2016 mean
2736
+ M 0.0 1.0 2.0 3.0 1.5
2737
+ F 4.0 5.0 6.0 7.0 5.5
2738
+ mean 2.0 3.0 4.0 5.0 3.5
2739
+
2740
+ Specifying an axis and a label
2741
+
2742
+ >>> arr.with_total('gender', label='U')
2743
+ gender\time 2013 2014 2015 2016
2744
+ M 0 1 2 3
2745
+ F 4 5 6 7
2746
+ U 4 6 8 10
2747
+
2748
+ Using groups
2749
+
2750
+ >>> time_groups = (arr.time[:2014] >> 'before_2015',
2751
+ ... arr.time[2015:] >> 'after_2015')
2752
+ >>> arr.with_total(time_groups)
2753
+ gender\time 2013 2014 2015 2016 before_2015 after_2015
2754
+ M 0 1 2 3 1 5
2755
+ F 4 5 6 7 9 13
2756
+ >>> # or equivalently
2757
+ >>> # arr.with_total('time[:2014] >> before_2015; time[2015:] >> after_2015')
2745
2758
"""
2746
2759
# TODO: default to op.__name__
2747
2760
label = kwargs .pop ('label' , 'total' )
0 commit comments