equal#
- equal(obj, obj2, *args, method=None, detailed=False, equal_nan=True, leaf=False, union=True, verbose=None, die=False, **kwargs)[source]#
Compare equality between two arbitrary objects
This method parses two (or more) objects of any type (lists, dictionaries, custom classes, etc.) and determines whether or not they are equal. By default it returns true/false for whether or not the objects match, but it can also return a detailed comparison of exactly which attributes (or keys, etc) match or don’t match between the two objects. It works by first parsing the entire object into “leaves” via
sc.iterobj()
, and then comparing each “leaf” via one of the methods described below.There is no universal way to check equality between objects in Python. Some objects define their own equals method which may not evaluate to true/false (e.g., Numpy arrays and pandas dataframes). For others it may be undefined. For this reasons, different ways of checking equality may give different results in edge cases. The available methods are:
'eq'
: uses the objects’ built-in__eq__()
methods (most accurate, but most likely to fail)'pickle'
: converts the object to a binary pickle (most robust)'json'
: converts the object to a JSON viajsonpickle
(gives most detailed object structure, but can be lossy)'str'
: converts the object to its string representation (least amount of detail)In addition, any custom function can be provided
By default, ‘eq’ is tried first, and if that raises an exception, ‘pickle’ is tried (equivalent to
method=['eq', 'pickle']
).- Parameters:
obj (any) – the first object to compare
obj2 (any) – the second object to compare
args (list) – additional objects to compare
method (str) – see above
detailed (int) – whether to compute a detailed comparison of the objects, and return a dataframe of the results (if detailed=2, return the value of each object as well)
equal_nan (bool) – whether matching
np.nan
should compare as true (default True; NB, False not guaranteed to work withmethod='pickle'
or'str'
, which includes the default; True not guaranteed to work withmethod='json'
)leaf (bool) – if True, only compare the object’s leaf nodes (those with no children); otherwise, compare everything
union (bool) – if True, construct the comparison tree as the union of the trees of each object (i.e., an extra attribute in one object will show up as an additional row in the comparison; otherwise rows correspond to the attributes of the first object)
verbose (bool) – level of detail to print
die (bool) – whether to raise an exception if an error is encountered (else return False)
kwargs (dict) – passed to
sc.iterobj()
Examples:
o1 = dict( a = [1,2,3], b = np.array([4,5,6]), c = dict( df = sc.dataframe(q=[sc.date('2022-02-02'), sc.date('2023-02-02')]) ) ) # Identical object o2 = sc.dcp(o1) # Non-identical object o3 = sc.dcp(o1) o3['b'][2] = 8 sc.equal(o1, o2) # Returns True sc.equal(o1, o3) # Returns False e = sc.Equal(o1, o2, o3, detailed=True) # Create an object e.df.disp() # Show results as a dataframe
New in version 3.1.0.New in version 3.1.3: “union” argument; more detailed output