agate.computations

This module contains the Computation class and its subclasses. Computations allow for row-wise calculation of new data for Table. For instance, the PercentChange subclass takes two column names as arguments and computes the percentage change between them for each row.

Computations are applied to tables using the Table.compute() method. For efficiencies sake, this method accepts a sequence of operations, which are applied simultaneously.

If the basic computations supplied in this module are not suitable to your needs the Formula subclass can be used to apply an arbitrary function to the data in each row. If this is still not suitable, Computation can be subclassed to fully customize it’s behavior.

class agate.computations.Computation

Bases: object

Base class for row-wise computations on a Table.

get_computed_data_type(table)

Returns an instantiated DataType which will be appended to the table.

prepare(table)

Called with the table immediately prior to invoking the computation with rows. Can be used to compute column-level statistics for computations. By default, this does nothing.

run(row)

When invoked with a row, returns the computed new column value.

class agate.computations.Formula(data_type, func)

Bases: agate.computations.Computation

A simple drop-in computation that can apply any function to rows.

get_computed_data_type(table)
run(row)
class agate.computations.Change(before_column_name, after_column_name)

Bases: agate.computations.Computation

Computes change between two columns.

get_computed_data_type(table)
prepare(table)
run(row)
class agate.computations.PercentChange(before_column_name, after_column_name)

Bases: agate.computations.Computation

Computes percent change between two columns.

get_computed_data_type(table)
prepare(table)
run(row)
class agate.computations.Rank(column_name, comparer=None, reverse=None)

Bases: agate.computations.Computation

Computes rank order of the values in a column.

Uses the “competition” ranking method: if there are four values and the middle two are tied, then the output will be [1, 2, 2, 4].

Null values will always be ranked last.

Parameters:
  • column_name – The name of the column to rank.
  • comparer – An optional comparison function. If not specified ranking will be ascending, with nulls ranked last.
  • reverse – Reverse sort order before ranking.
get_computed_data_type(table)
prepare(table)
run(row)
class agate.computations.PercentileRank(column_name, comparer=None, reverse=None)

Bases: agate.computations.Rank

Assign each value in a column to the percentile into which it falls.

See Percentiles for implementation details.

prepare(table)
run(row)