Repository: capitalone/datacompy

Issue:
Max Diff in Report not working if comparing object and numeric datatypes
Although the tolerance values and overall matching seem to work fine when comparing object to float64 or int64, in the summary report, the Max Diff is always 0.000000

Probably can be fixed here with .astype(float)?: https://github.com/capitalone/datacompy/blob/4d7fc4889322e22f487d7cf33cb1554fce3a496d/datacompy/core.py#L295-L297



Discussion:

Patch:
diff --git a/.gitignore b/.gitignore
index 432450f..588be13 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,6 +6,7 @@ dist/
 .idea/
 *.pyc
 .DS_Store
+.pytest_cache/
 
 # Spark junk
 metastore_db/
@@ -14,4 +15,4 @@ derby.log
 
 *.ipynb*
 docs/doctrees
-docs/.buildinfo
\ No newline at end of file
+docs/.buildinfo
diff --git a/datacompy/core.py b/datacompy/core.py
index e03d75e..0cb0a72 100644
--- a/datacompy/core.py
+++ b/datacompy/core.py
@@ -290,14 +290,8 @@ class Compare(object):
                     self.abs_tol,
                     ignore_spaces)
                 match_cnt = self.intersect_rows[col_match].sum()
-
-                try:
-                    max_diff = (
-                        self.intersect_rows[col_1]
-                        - self.intersect_rows[col_2]).abs().max()
-                except:
-                    max_diff = 0
-
+                max_diff = calculate_max_diff(
+                    self.intersect_rows[col_1], self.intersect_rows[col_2])
                 null_diff = (
                     (self.intersect_rows[col_1].isnull())
                     ^ (self.intersect_rows[col_2].isnull())).sum()
@@ -626,7 +620,7 @@ def columns_equal(col_1, col_2, rel_tol=0, abs_tol=0, ignore_spaces=False):
                         col_1 = col_1.str.strip()
                     if col_2.dtype.kind == 'O':
                         col_2 = col_2.str.strip()
-                        
+
                 if set([col_1.dtype.kind, col_2.dtype.kind]) == set(['M','O']):
                     compare = compare_string_and_date_columns(col_1, col_2)
                 else:
@@ -718,3 +712,23 @@ def temp_column_name(*dataframes):
                 unique = False
         if unique:
             return temp_column
+
+def calculate_max_diff(col_1, col_2):
+    """Get a maximum difference between two columns
+
+    Parameters
+    ----------
+    col_1 : Pandas.Series
+        The first column
+    col_2 : Pandas.Series
+        The second column
+
+    Returns
+    -------
+    Numeric
+        Numeric field, or zero.
+    """
+    try:
+        return (col_1.astype(float) - col_2.astype(float)).abs().max()
+    except:
+        return 0


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index a5eb6e1..329a2df 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -677,3 +677,26 @@ def test_index_with_joins_with_ignore_spaces():
     assert compare.all_columns_match()
     assert compare.all_rows_overlap()
     assert compare.intersect_rows_match()
+
+
+MAX_DIFF_DF = pd.DataFrame({
+    'base': [1,1,1,1,1],
+    'floats': [1.1,1.1,1.1,1.2,0.9],
+    'decimals': [Decimal('1.1'), Decimal('1.1'), Decimal('1.1'), Decimal('1.1'), Decimal('1.1')],
+    'null_floats': [np.nan, 1.1, 1, 1, 1],
+    'strings': ['1', '1', '1', '1.1', '1'],
+    'mixed_strings': ['1','1','1','2','some string'],
+    'infinity': [1,1,1,1,np.inf]
+    })
+
+@pytest.mark.parametrize('column,expected', [
+    ('base', 0),
+    ('floats', 0.2),
+    ('decimals', 0.1),
+    ('null_floats', 0.1),
+    ('strings', 0.1),
+    ('mixed_strings', 0),
+    ('infinity', np.inf)
+])
+def test_calculate_max_diff(column, expected):
+    assert np.isclose(datacompy.calculate_max_diff(MAX_DIFF_DF['base'], MAX_DIFF_DF[column]), expected)