Code/5-data-cleansing/clean_met.py
def clean_date(data):
    """
    Clean Object Date column using Regex
    """
    column_info(data, "Object Date")

    year_pattern = "(\d){3,4}"
    year_pattern_regex = re.compile(year_pattern)

    data_year_mo = data["Object Date"].apply(lambda x: x if x is np.NaN else
                                             year_pattern_regex.search(x))

    data["Object Date Clean"] = data_year_mo.apply(lambda x: x if x is None or
                                                   x is np.NaN else x.group(0))

    data_dates = data[["Object Date", "Object Date Clean"]]

    logging.info(data_dates)

    century_pattern = "(?P<century>\d\d)(th (C|c)entury)"
    century_pattern_regex = re.compile(century_pattern)

    data_century_match = data_dates.apply(
        lambda x: x["Object Date Clean"] if x["Object Date Clean"] is not None
        else x["Object Date"] if x["Object Date"] is np.NaN else
        century_pattern_regex.search(x["Object Date"]),
        axis=1,
    )

    data["Object Date Clean"] = data_century_match.apply(
        lambda x: x if isinstance(x, (str, type(None), type(np.NaN))) else (
            int(x.group("century")) - 1) * 100)

    logging.info(data[["Object Date", "Object Date Clean"]])

    data["Object Date Clean"] = data.apply(lambda x: 0 if x["Object Date"] is
                                           np.NaN else x["Object Date Clean"],
                                           axis=1)
    logging.info(data[["Object Date", "Object Date Clean"]])

    logging.info(data.loc[data["Object Date Clean"].isnull(
    ), ["Object Date", "Object Date Clean"]])

    column_info(data, "Object Date")
    column_info(data, "Object Date Clean")
    return data

    Clean Object Date column using Regex
    
Clean Object Date column using Regex
Clean Object Date column using Regex
def clean_date(data):
    column_info(data, "Object Date")
    year_pattern = "(\d){3,4}"
    year_pattern_regex = re.compile(year_pattern)
    data_year_mo = data["Object Date"].apply(lambda x: x if x is np.NaN else
                                             year_pattern_regex.search(x))
    data["Object Date Clean"] = data_year_mo.apply(lambda x: x if x is None or
                                                   x is np.NaN else x.group(0))
    data_dates = data[["Object Date", "Object Date Clean"]]
    logging.info(data_dates)
    century_pattern = "(?P<century>\d\d)(th (C|c)entury)"
    century_pattern_regex = re.compile(century_pattern)
    data_century_match = data_dates.apply(
        lambda x: x["Object Date Clean"] if x["Object Date Clean"] is not None
        else x["Object Date"] if x["Object Date"] is np.NaN else
        century_pattern_regex.search(x["Object Date"]),
        axis=1,
    )
    data["Object Date Clean"] = data_century_match.apply(
        lambda x: x if isinstance(x, (str, type(None), type(np.NaN))) else (
            int(x.group("century")) - 1) * 100)
    logging.info(data[["Object Date", "Object Date Clean"]])
    data["Object Date Clean"] = data.apply(lambda x: 0 if x["Object Date"] is
                                           np.NaN else x["Object Date Clean"],
                                           axis=1)
    logging.info(data[["Object Date", "Object Date Clean"]])
    logging.info(data.loc[data["Object Date Clean"].isnull(
    ), ["Object Date", "Object Date Clean"]])
    column_info(data, "Object Date")
    column_info(data, "Object Date Clean")
    return data
def clean_date ( data ) : column_info ( data , "Object Date" ) year_pattern = "(\d){3,4}" year_pattern_regex = re . compile ( year_pattern ) data_year_mo = data [ "Object Date" ] . apply ( lambda x : x if x is np . NaN else year_pattern_regex . search ( x ) ) data [ "Object Date Clean" ] = data_year_mo . apply ( lambda x : x if x is None or x is np . NaN else x . group ( 0 ) ) data_dates = data [ [ "Object Date" , "Object Date Clean" ] ] logging . info ( data_dates ) century_pattern = "(?P<century>\d\d)(th (C|c)entury)" century_pattern_regex = re . compile ( century_pattern ) data_century_match = data_dates . apply ( lambda x : x [ "Object Date Clean" ] if x [ "Object Date Clean" ] is not None else x [ "Object Date" ] if x [ "Object Date" ] is np . NaN else century_pattern_regex . search ( x [ "Object Date" ] ) , axis = 1 , ) data [ "Object Date Clean" ] = data_century_match . apply ( lambda x : x if isinstance ( x , ( str , type ( None ) , type ( np . NaN ) ) ) else ( int ( x . group ( "century" ) ) - 1 ) * 100 ) logging . info ( data [ [ "Object Date" , "Object Date Clean" ] ] ) data [ "Object Date Clean" ] = data . apply ( lambda x : 0 if x [ "Object Date" ] is np . NaN else x [ "Object Date Clean" ] , axis = 1 ) logging . info ( data [ [ "Object Date" , "Object Date Clean" ] ] ) logging . info ( data . loc [ data [ "Object Date Clean" ] . isnull ( ) , [ "Object Date" , "Object Date Clean" ] ] ) column_info ( data , "Object Date" ) column_info ( data , "Object Date Clean" ) return data
Clean Object Date column using Regex
Clean Object Date column using Regex
"""
    Clean Object Date column using Regex
    """
data