1
\$\begingroup\$

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

best regards,

Jacob Collstrup

\$\endgroup\$
  • 2
    \$\begingroup\$ Can you give some example IO and explain what the code is doing? I understand your practicing some data science but that doesn't explain the code. Additionally titles should only consist of a description of your code. \$\endgroup\$ – Peilonrayz Jul 6 at 18:42
1
\$\begingroup\$

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, "%Y-%m-%d")
       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
| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.