Skip to content

weighted_percentile should error/warn when all sample weights 0 #31032

@lucyleeow

Description

@lucyleeow

Describe the bug

Noticed while working on #29431

Steps/Code to Reproduce

See the following test:

def test_weighted_percentile_zero_weight():
y = np.empty(102, dtype=np.float64)
y.fill(1.0)
sw = np.ones(102, dtype=np.float64)
sw.fill(0.0)
value = _weighted_percentile(y, sw, 50)
assert approx(value) == 1.0

Expected Results

Error or warning should probably be given. You're effectively asking for a quantile of a empty array.

Actual Results

When all sample weights are 0, what happens is that percentile_in_sorted (as in the index of desired observation in array is the) is 101 (the last item). We should probably add a check and give a warning when sample_weights is all zero

cc @ogrisel @glemaitre

Versions

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions