table_enforcer package

Submodules

table_enforcer.errors module

Provide error classes.

exception table_enforcer.errors.NotImplementedYet(msg=None)[source]

Bases: NotImplementedError, table_enforcer.errors.TableEnforcerError

Raise when a section of code that has been left for another time is asked to execute.

__init__(msg=None)[source]

Set up the Exception.

exception table_enforcer.errors.RecodingError(column, recoder, exception)[source]

Bases: table_enforcer.errors.TableEnforcerError

Raise when a recoder function raises an error.

__init__(column, recoder, exception)[source]

Set up the Exception.

exception table_enforcer.errors.TableEnforcerError[source]

Bases: Exception

Base error class.

exception table_enforcer.errors.ValidationError[source]

Bases: table_enforcer.errors.TableEnforcerError

Raise when a validator function fails to generate all successes when called inside of a recode method.

table_enforcer.main_classes module

Main module.

class table_enforcer.main_classes.Enforcer(columns)[source]

Bases: object

Class to define table definitions.

__init__(columns)[source]

Initialize an enforcer instance.

_make_validations(table: pandas.core.frame.DataFrame) → box.Box[source]

Return a dict-like object containing dataframes of which tests passed/failed for each column.

recode(table: pandas.core.frame.DataFrame, validate=False) → pandas.core.frame.DataFrame[source]

Return a fully recoded dataframe.

Parameters:
  • table (pd.DataFrame) – A dataframe on which to apply recoding logic.
  • validate (bool) – If True, recoded table must pass validation tests.
validate(table: pandas.core.frame.DataFrame) → bool[source]

Return True if all validation tests pass: False otherwise.

class table_enforcer.main_classes.BaseColumn[source]

Bases: object

Base Class for Columns.

Lays out essential methods api.

recode(table: pandas.core.frame.DataFrame, validate=False) → pandas.core.frame.DataFrame[source]

Pass the appropriate columns through each recoder function sequentially and return the final result.

Parameters:
  • table (pd.DataFrame) – A dataframe on which to apply recoding logic.
  • validate (bool) – If True, recoded table must pass validation tests.
update_dataframe(df, table, validate=False)[source]

Perform self.recode and add resulting column(s) to df and return df.

validate(table: pandas.core.frame.DataFrame, failed_only=False) → pandas.core.frame.DataFrame[source]

Return a dataframe of validation results for the appropriate series vs the vector of validators.

Parameters:
  • table (pd.DataFrame) – A dataframe on which to apply validation logic.
  • failed_only (bool) – If True: return only the indexes that failed to validate.
class table_enforcer.main_classes.Column(name: str, dtype: type, unique: bool, validators: typing.List[typing.Callable[pandas.core.series.Series, pandas.core.frame.DataFrame]], recoders: typing.List[typing.Callable[pandas.core.series.Series, pandas.core.series.Series]]) → None[source]

Bases: table_enforcer.main_classes.BaseColumn

Class representing a single table column.

__init__(name: str, dtype: type, unique: bool, validators: typing.List[typing.Callable[pandas.core.series.Series, pandas.core.frame.DataFrame]], recoders: typing.List[typing.Callable[pandas.core.series.Series, pandas.core.series.Series]]) → None[source]

Construct a new Column object.

Parameters:
  • name (str) – The exact name of the column in a pd.DataFrame.
  • dtype (type) – The type that each member of the recoded column must belong to.
  • unique (bool) – Whether values are allowed to recur in this column.
  • validators (list) – A list of validator functions.
  • recoders (list) – A list of recoder functions.
_dict_of_funcs(funcs: list) → pandas.core.series.Series[source]

Return a pd.Series of functions with index derived from the function name.

_validate_series_dtype(series: pandas.core.series.Series) → pandas.core.series.Series[source]

Validate that the series data is the correct dtype.

recode(table: pandas.core.frame.DataFrame, validate=False) → pandas.core.frame.DataFrame[source]

Pass the provided series obj through each recoder function sequentially and return the final result.

Parameters:
  • table (pd.DataFrame) – A dataframe on which to apply recoding logic.
  • validate (bool) – If True, recoded table must pass validation tests.
validate(table: pandas.core.frame.DataFrame, failed_only=False) → pandas.core.frame.DataFrame[source]

Return a dataframe of validation results for the appropriate series vs the vector of validators.

Parameters:
  • table (pd.DataFrame) – A dataframe on which to apply validation logic.
  • failed_only (bool) – If True: return only the indexes that failed to validate.
class table_enforcer.main_classes.CompoundColumn(input_columns: typing.List[table_enforcer.main_classes.Column], output_columns: typing.List[table_enforcer.main_classes.Column], column_transform) → None[source]

Bases: table_enforcer.main_classes.BaseColumn

Class representing multiple columns and the logic governing their transformation from source table to recoded table.

__init__(input_columns: typing.List[table_enforcer.main_classes.Column], output_columns: typing.List[table_enforcer.main_classes.Column], column_transform) → None[source]

Construct a new CompoundColumn object.

Parameters:
  • input_columns (list, Column) – A list of Column objects representing column(s) from the SOURCE table.
  • output_columns (list, Column) – A list of Column objects representing column(s) from the FINAL table.
  • column_transform (Callable) – Function accepting the table object, performing transformations to it and returning a DataFrame containing the NEW columns only.
_do_validation_set(table: pandas.core.frame.DataFrame, columns, validation_type, failed_only=False) → pandas.core.frame.DataFrame[source]

Return a dataframe of validation results for the appropriate series vs the vector of validators.

_validate_input(table: pandas.core.frame.DataFrame, failed_only=False) → pandas.core.frame.DataFrame[source]

Return a dataframe of validation results for the appropriate series vs the vector of validators.

recode(table: pandas.core.frame.DataFrame, validate=False) → pandas.core.frame.DataFrame[source]

Pass the appropriate columns through each recoder function sequentially and return the final result.

Parameters:
  • table (pd.DataFrame) – A dataframe on which to apply recoding logic.
  • validate (bool) – If True, recoded table must pass validation tests.
validate(table: pandas.core.frame.DataFrame, failed_only=False) → pandas.core.frame.DataFrame[source]

Return a dataframe of validation results for the appropriate series vs the vector of validators.

Parameters:
  • table (pd.DataFrame) – A dataframe on which to apply validation logic.
  • failed_only (bool) – If True: return only the indexes that failed to validate.

Module contents

Top-level package for Table Enforcer.