Share(rule, missing=CountedMissing.EXCLUDED)
Source¶
Bases: Statistic
Fraction of the values satisfying a rule, between 0 and 1, without the missing values by default.
It gives the share of each group in aggregate and pivot_table without adding a column first.
Added in version 0.2.0
Parameters:
-
rule(Rule)The
Rulethe counted values satisfy, such asndp.Between(65, 99): seeRulefor every rule. -
missing(CountedMissing, default:CountedMissing.EXCLUDED)CountedMissing.EXCLUDEDby default to divide by the present values only,CountedMissing.INCLUDEDto divide by every value, a missing value then counting as not satisfying the rule.
Raises:
-
InvalidTypeErrorWhen a setting is not of the type its annotation names.
Examples:
import numdaplot as ndp
ages = ndp.Array.from_list([30.0, 70.0, None, 80.0])
ages.compute(ndp.Share(rule=ndp.Between(65, 99))).to_value()
# 0.6666666666666666
ages.compute(ndp.Share(rule=ndp.Between(65, 99), missing=ndp.CountedMissing.INCLUDED)).to_value()
# 0.5
The share of each group of rows.
sexes = ndp.Array.from_list([1, 2, 2, 1])
people = ndp.Table.from_arrays({"sex": sexes, "age": ages})
senior_share = ndp.Calculation(column="age", function=ndp.Share(rule=ndp.Between(65, 99)))
people.aggregate(ndp.Aggregation(calculations=[senior_share], group_by=["sex"]))
# sex age_share
# INT64 FLOAT64
# 0 1 0.5
# 1 2 1.0