tabled.compare_tables#

Tools to compare tables

Functions

columns_diff(df1, df2)

Return columns that are not common between df1 and df2.

columns_value_diff(df1, df2)

For each column present in both dataframes, compare the values row-wise.

dataframe_diffs(df1, df2[, comparisons, ...])

Compare the diff of dataframes using specified diff comparison functions.

dtypes_diff(df1, df2)

Return columns where the data types differ between df1 and df2.

ensure_comparisons_dict(comparisons)

Ensure that the comparisons are in the form of a dictionary.

index_diff(df1, df2)

Return indices that are not common between df1 and df2.

shape_diff(df1, df2)

Return the shapes of df1 and df2 if they differ.

Classes

BinaryFuncResult

A {left_right, right_left} dict, truthy if either value is truthy.

Exceptions

InvalidComparison

Raised or returned when a comparison that was asked for is not applicable to the dataframes in question

class tabled.compare_tables.BinaryFuncResult[source]#

Bases: dict

A {left_right, right_left} dict, truthy if either value is truthy.

classmethod from_func(func, x, y)[source]#

Build a BinaryFuncResult from func(x, y) and func(y, x).

exception tabled.compare_tables.InvalidComparison[source]#

Bases: ValueError

Raised or returned when a comparison that was asked for is not applicable to the dataframes in question

tabled.compare_tables.columns_diff(df1, df2)[source]#

Return columns that are not common between df1 and df2.

tabled.compare_tables.columns_value_diff(df1, df2)[source]#

For each column present in both dataframes, compare the values row-wise. Returns a dict with column names as keys and DataFrames of differences as values. Only columns with differences are included.

tabled.compare_tables.dataframe_diffs(df1, df2, comparisons={'columns_diff': <function columns_diff>, 'columns_value_diff': <function columns_value_diff>, 'dtypes_diff': <function dtypes_diff>, 'index_diff': <function index_diff>, 'shape_diff': <function shape_diff>}, *, diff_condition=<class 'bool'>)[source]#

Compare the diff of dataframes using specified diff comparison functions.

Returns a dictionary with comparison names as keys and comparison results as values.

Parameters:
  • df1 (DataFrame) – The first dataframe to compare.

  • df2 (DataFrame) – The second dataframe to compare.

  • comparisons (Union[dict[str, Callable[[DataFrame, DataFrame], Any]], Sequence[Union[str, Callable[[DataFrame, DataFrame], Any]]], str, Callable[[DataFrame, DataFrame], Any]]) – A dictionary or list of comparison functions or names. Defaults to DFLT_COMPARISONS.

  • diff_condition (callable) – A function that determines whether to include a comparison result in the output dictionary based on the comparison result. Defaults to the built-in bool function.

Returns:

A dictionary with comparison names as keys and comparison results as values.

Return type:

dict

Example

>>> import pandas as pd
>>> df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=[0, 1])
>>> df2 = pd.DataFrame({'A': [1, 2], 'C': [5, 6]}, index=[1, 2])
>>> diffs = dataframe_diffs(df1, df2)
>>> diffs
{'columns_diff': {'left_right': {'B'}, 'right_left': {'C'}},
 'index_diff': {'left_right': {0}, 'right_left': {2}},
 'columns_value_diff': {'A':    left  right
1     2      1}}
tabled.compare_tables.dtypes_diff(df1, df2)[source]#

Return columns where the data types differ between df1 and df2.

tabled.compare_tables.ensure_comparisons_dict(comparisons)[source]#

Ensure that the comparisons are in the form of a dictionary.

Return type:

dict[str, Callable[[DataFrame, DataFrame], Any]]

tabled.compare_tables.index_diff(df1, df2)[source]#

Return indices that are not common between df1 and df2.

tabled.compare_tables.shape_diff(df1, df2)[source]#

Return the shapes of df1 and df2 if they differ.