Skip to content

Commit 732de7d

Browse files
committed
add the test
1 parent 88a0bc3 commit 732de7d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pandas/tests/frame/methods/test_sample.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ def test_sample_inf_weights(self, obj):
137137
with pytest.raises(ValueError, match=msg):
138138
obj.sample(n=3, weights=weights_with_ninf)
139139

140+
def test_sample_unit_probabilities_raises(self, obj):
141+
# GH#61516
142+
high_variance_weights = [1] * 10
143+
high_variance_weights[0] = 100
144+
msg = (
145+
"Invalid weights: If `replace`=False,"
146+
" total unit probabilities have to be less than 1"
147+
)
148+
with pytest.raises(ValueError, match=msg):
149+
obj.sample(n=2, weights=high_variance_weights, replace=False)
150+
151+
# edge case, n*max(weights)/sum(weights) == 1
152+
edge_variance_weights = [1] * 10
153+
edge_variance_weights[0] = 9
154+
# should not raise
155+
obj.sample(n=2, weights=edge_variance_weights, replace=False)
156+
157+
low_variance_weights = [1] * 10
158+
low_variance_weights[0] = 8
159+
# should not raise
160+
obj.sample(n=2, weights=low_variance_weights, replace=False)
161+
162+
140163
def test_sample_zero_weights(self, obj):
141164
# All zeros raises errors
142165

0 commit comments

Comments
 (0)