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

An operation that takes a table and produces a new column by performing some computation on each row. Computations are invoked with TableSet.compute.

When implementing a custom subclass, ensure that the values returned by run() are of the type specified by get_computed_data_type(). This can be ensured by using the DataType.cast() method. See Formula for an example.

get_computed_data_type(table)

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

validate(table)

Perform any checks necessary to verify this computation can run on the provided table without errors. This is called by Table.compute() before run().

run(table)

When invoked with a table, returns a sequence of new column values.

class agate.computations.Formula(data_type, func, cast=True)

Bases: agate.computations.Computation

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

Parameters:
  • data_type – The data type this formula will return.
  • func – The function to be applied to each row. Must return a valid value for the specified data type.
  • cast – If True, each return value will be cast to the specified data_type to ensure it is valid. Only specify false if you are certain your formula always returns the correct type.
get_computed_data_type(table)
run(table)
class agate.computations.Change(before_column_name, after_column_name)

Bases: agate.computations.Computation

Computes change between two columns.

get_computed_data_type(table)
validate(table)
run(table)
class agate.computations.Percent(column_name, total=None)

Bases: agate.computations.Computation

Computes a column’s percentage of a total

get_computed_data_type(table)
validate(table)
run(table)
Returns:decimal.Decimal
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)
validate(table)
run(table)
Returns:decimal.Decimal
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)
run(table)
Returns:int
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.

validate(table)
run(table)
Returns:int