Check that elements in list follow a specified date-format schema and are not null/na/nan 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=&quot;ignore&quot;, format= &quot;%d-%m-%Y&quot;), date):
            counter += 1
            true_dates.append(pd.to_datetime(element, errors=&quot;raise&quot;, format= &quot;%Y-%m-%d&quot;))
        else:
            counter +=1
            false_dates.append(element)
            tracker.append(counter)

    if len(tracker) == 0:
        return &quot;column is ok.&quot;
    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
best regards,
Jacob Collstrup
One of my friends on Discord helped me out. He didn't like me attempt either! =oP
I created this function:
import datetime

def str2date(string):
   try:
       datetime_holder = datetime.datetime.strptime(string, &quot;%Y-%m-%d&quot;)
       return datetime_holder.date()
   except ValueError:
       return string
   except TypeError:
       return string

And looped over it like this:
def datetime_check_format(col):
tracker = []
false_dates = []
true_dates = []
counter = -1
for element in col:
    counter +=1
    if isinstance(str2date(element), date):
        true_dates.append(str2date(element))
    else:
        tracker.append(counter)
        false_dates.append(str2date(element))

return tracker, false_dates, true_dates