File size: 1,198 Bytes
19641b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ============================================================
# utils/normalizers.py
# ============================================================

import pandas as pd


def normalize_value(

    value,

    ignore_spaces=True,

    match_case=False,

    relaxed_decimals=True,

    normalize_dates=True,

    treat_blank_as_null=True

):
    """

    Standardizes values before comparison.

    This helps avoid false mismatches such as:

    - "ABC " vs "ABC"

    - "100.00" vs "100"

    - "01/01/2024" vs "2024-01-01"

    - blank vs NULL

    """

    if pd.isna(value):
        return None

    value = str(value)

    if ignore_spaces:
        value = value.strip()

    if treat_blank_as_null and value == "":
        return None

    if not match_case:
        value = value.lower()

    if relaxed_decimals:
        try:
            number = float(value)
            return round(number, 6)
        except Exception:
            pass

    if normalize_dates:
        try:
            date_value = pd.to_datetime(value, errors="raise")
            return date_value.strftime("%Y-%m-%d")
        except Exception:
            pass

    return value