Skip to content

Commit 23926f6

Browse files
committed
add new tests
1 parent 92122f3 commit 23926f6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pandas/tests/frame/methods/test_sample.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ def test_sample_inf_weights(self, obj):
134134
with pytest.raises(ValueError, match=msg):
135135
obj.sample(n=3, weights=weights_with_ninf)
136136

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

0 commit comments

Comments
 (0)