我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用statistics.median_grouped()。
def MEDIAN_GROUPED(df, n, price='Close', interval=1): """ Median, or 50th percentile, of grouped data """ median_grouped_list = [] i = 0 while i < len(df[price]): if i + 1 < n: median_grouped = float('NaN') else: start = i + 1 - n end = i + 1 median_grouped = statistics.median_grouped(df[price][start:end], interval) median_grouped_list.append(median_grouped) i += 1 return median_grouped_list
def main(): print(stats.mean(range(6))) print(stats.median(range(6))) print(stats.median_low(range(6))) print(stats.median_high(range(6))) print(stats.median_grouped(range(6))) try: print(stats.mode(range(6))) except Exception as e: print(e) print(stats.mode(list(range(6)) + [3])) print(stats.pstdev(list(range(6)) + [3])) print(stats.stdev(list(range(6)) + [3])) print(stats.pvariance(list(range(6)) + [3])) print(stats.variance(list(range(6)) + [3]))
def test_repeated_single_value(self): # Override method from AverageMixin. # Yet again, failure of median_grouped to conserve the data type # causes me headaches :-( for x in (5.3, 68, 4.3e17, Fraction(29, 101), Decimal('32.9714')): for count in (2, 5, 10, 20): data = [x]*count self.assertEqual(self.func(data), float(x))
def test_odd_fractions(self): # Test median_grouped works with an odd number of Fractions. F = Fraction data = [F(5, 4), F(9, 4), F(13, 4), F(13, 4), F(17, 4)] assert len(data)%2 == 1 random.shuffle(data) self.assertEqual(self.func(data), 3.0)
def test_even_fractions(self): # Test median_grouped works with an even number of Fractions. F = Fraction data = [F(5, 4), F(9, 4), F(13, 4), F(13, 4), F(17, 4), F(17, 4)] assert len(data)%2 == 0 random.shuffle(data) self.assertEqual(self.func(data), 3.25)
def test_odd_decimals(self): # Test median_grouped works with an odd number of Decimals. D = Decimal data = [D('5.5'), D('6.5'), D('6.5'), D('7.5'), D('8.5')] assert len(data)%2 == 1 random.shuffle(data) self.assertEqual(self.func(data), 6.75)
def test_even_decimals(self): # Test median_grouped works with an even number of Decimals. D = Decimal data = [D('5.5'), D('5.5'), D('6.5'), D('6.5'), D('7.5'), D('8.5')] assert len(data)%2 == 0 random.shuffle(data) self.assertEqual(self.func(data), 6.5) #--- data = [D('5.5'), D('5.5'), D('6.5'), D('7.5'), D('7.5'), D('8.5')] assert len(data)%2 == 0 random.shuffle(data) self.assertEqual(self.func(data), 7.0)
def median_grouped(text): """ Finds the grouped median of a space-separated list of numbers. Calculates the median of grouped continuous data, calculated as the 50th percentile, using interpolation. Example:: /median grouped 33 54 43 65 43 62 """ return format_output(statistics.median_grouped(parse_numeric_list(text)))
def setup(): commands.add(mean) commands.add(median) commands.add(median_low) commands.add(median_high) commands.add(median_grouped) commands.add(mode) commands.add(pstdev) commands.add(pvariance) commands.add(stdev) commands.add(variance)