def run(self, table): """ :returns: :class:`int` """ percentiles = Percentiles(self._column_name).run(table) new_column = [] for row in table.rows: new_column.append(percentiles.locate(row[self._column_name])) return new_column
class PercentileRank(Rank): """ Assign each value in a column to the percentile into which it falls. See :class:`.Percentiles` for implementation details. """ def prepare(self, table): column = table.columns[self._column_name] if not isinstance(column.data_type, Number): raise DataTypeError('PercentileRank column must contain Number data.') self._percentiles = Percentiles(self._column_name).run(table) def run(self, row): """ :returns: :class:`int` """ return self._percentiles.locate(row[self._column_name])
class PercentileRank(Rank): """ Assign each value in a column to the percentile into which it falls. See :class:`.Percentiles` for implementation details. """ def prepare(self, table): column = table.columns[self._column_name] if not isinstance(column.data_type, Number): raise DataTypeError( 'PercentileRank column must contain Number data.') self._percentiles = Percentiles(self._column_name).run(table) def run(self, row): """ :returns: :class:`int` """ return self._percentiles.locate(row[self._column_name])
def test_percentiles_locate(self): rows = [(n, ) for n in range(1, 1001)] table = Table(rows, ['ints'], [self.number_type]) percentiles = Percentiles('ints').run(table) self.assertEqual(percentiles.locate(251), Decimal('25')) self.assertEqual(percentiles.locate(260), Decimal('25')) self.assertEqual(percentiles.locate(261), Decimal('26')) with self.assertRaises(ValueError): percentiles.locate(0) with self.assertRaises(ValueError): percentiles.locate(1012)