I'm practicing some data-analysis and I'm currently checking the integrity of my data. The elements that do not follow my date-schema, should be funneled to a new separate list so I can work on strategies and suggestions on how to handle these exceptions.
The following code checks the format successfully:
def datetime_check_format(col): tracker =  false_dates =  true_dates =  counter = 0 for element in col: if not isinstance(pd.to_datetime(element, errors="ignore", format= "%d-%m-%Y"), date): counter += 1 true_dates.append(pd.to_datetime(element, errors="raise", format= "%Y-%m-%d")) else: counter +=1 false_dates.append(element) tracker.append(counter) if len(tracker) == 0: return "column is ok." else: return tracker, false_dates, true_dates
I was wondering if someone has an idea on how to make this code better. It seems to be based on some backwards bending mobius-ring kind of twisted logic.
I used this one as guide, since it told me of error-handling: pandas.to_datetime