mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
bpo-38308: Add optional weighting to statistics.harmonic_mean() (GH-23914)
This commit is contained in:
parent
6dd3da3cf4
commit
cc3467a57b
4 changed files with 62 additions and 27 deletions
|
@ -1599,6 +1599,27 @@ class TestHarmonicMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
|
|||
actual = self.func(data*2)
|
||||
self.assertApproxEqual(actual, expected)
|
||||
|
||||
def test_with_weights(self):
|
||||
self.assertEqual(self.func([40, 60], [5, 30]), 56.0) # common case
|
||||
self.assertEqual(self.func([40, 60],
|
||||
weights=[5, 30]), 56.0) # keyword argument
|
||||
self.assertEqual(self.func(iter([40, 60]),
|
||||
iter([5, 30])), 56.0) # iterator inputs
|
||||
self.assertEqual(
|
||||
self.func([Fraction(10, 3), Fraction(23, 5), Fraction(7, 2)], [5, 2, 10]),
|
||||
self.func([Fraction(10, 3)] * 5 +
|
||||
[Fraction(23, 5)] * 2 +
|
||||
[Fraction(7, 2)] * 10))
|
||||
self.assertEqual(self.func([10], [7]), 10) # n=1 fast path
|
||||
with self.assertRaises(TypeError):
|
||||
self.func([1, 2, 3], [1, (), 3]) # non-numeric weight
|
||||
with self.assertRaises(statistics.StatisticsError):
|
||||
self.func([1, 2, 3], [1, 2]) # wrong number of weights
|
||||
with self.assertRaises(statistics.StatisticsError):
|
||||
self.func([10], [0]) # no non-zero weights
|
||||
with self.assertRaises(statistics.StatisticsError):
|
||||
self.func([10, 20], [0, 0]) # no non-zero weights
|
||||
|
||||
|
||||
class TestMedian(NumericTestCase, AverageMixin):
|
||||
# Common tests for median and all median.* functions.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue