Skip to content

Commit 5a76a7d

Browse files
committed
fix #693 : updated documentation of with_total
1 parent 349006f commit 5a76a7d

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

larray/core/array.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,9 +2693,8 @@ def _aggregate(self, op, args, kwargs=None, keepaxes=False, by_agg=False, commut
26932693
res = func(op, axes, keepaxes=keepaxes, out=out, **extra_kwargs)
26942694
return res
26952695

2696-
# op=sum does not parse correctly
26972696
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)
26992698
27002699
Add aggregated values (sum by default) along each axis.
27012700
A user defined label can be given to specified the computed values.
@@ -2706,7 +2705,8 @@ def with_total(self, *args, **kwargs):
27062705
Axes or groups along which to compute the aggregates. Passed groups should be named.
27072706
Defaults to aggregate over the whole array.
27082707
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`.
27102710
label : scalar value, optional
27112711
Label to use for the total. Applies only to aggregated axes, not groups. Defaults to "total".
27122712
**kwargs : int or str or Group or any combination of those, optional
@@ -2718,30 +2718,43 @@ def with_total(self, *args, **kwargs):
27182718
27192719
Examples
27202720
--------
2721-
>>> arr = ndtest((3, 3))
2721+
>>> arr = ndtest("gender=M,F;time=2013..2016")
27222722
>>> 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
27272726
>>> 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')
27452758
"""
27462759
# TODO: default to op.__name__
27472760
label = kwargs.pop('label', 'total')

0 commit comments

Comments
 (0)