0

I'm mapping date formats using regex, and I want to convert my dates to "%Y-%m-%d"

When testing with "%Y-%m-%d %H:%M:%S", my dates don't convert correctly. Can anyone see what I'm doing wrong? I have no idea how to deal with the unconverted data remaining.

Here is the test code for your reference.

from datetime import datetime
import re
import pandas as pd


def conv_date(dte: str) -> datetime: #actul is datetime
    acceptable_mappings = {
        "\d{4}-\d{2}-\d{2}": "%Y-%m-%d",
        "\d{2}-\d{2}-\d{4}": "%d-%m-%Y",
        "\d{4}/\d{2}/\d{2}": "%Y/%m/%d",
        "\d{2}/\d{2}/\d{4}": "%d/%m/%Y",
        "\d{8}": '%d%m%Y',
        "\d{2}\s\d{2}\s\d{4}": '%d %m %Y',
        "\d{4}-\d{2}-\d{2}\s\d{2}\:\d{2}\:\d{2}": "%Y-%m-%d %H:%M:%S",

    }
    for regex in acceptable_mappings.keys():
        if re.match(regex, dte):
            return datetime.strptime(dte, acceptable_mappings[regex])
    raise Exception(f"Expected date in one of supported formats, got {dte}")


def full_list_parse(unclean_list: list) -> list:
    return [conv_date(dte) for dte in unclean_list]


mock_dict = [
    {"name": "dz", "role": "legend", "date": "2023-07-26"},
    {"name": "mc", "role": "sounds like a dj", "date": "26-07-2023"},
    {"name": "xc", "role": "loves xcom", "date": "2023/07/26"},
    {"name": "lz", "role": "likes a to fly", "date": "26/07/2023"},
    {"name": "wc", "role": "has a small bladder", "date": "26072023"},
    {"name": "aa", "role": "warrior of the crystal", "date": "26 07 2023"},
    {"name": "xx", "role": "loves only-fans", "date": "2023-07-26 12:46:21"},

]

df = pd.DataFrame(mock_dict)

if __name__ == "__main__":
    print(df)
    df['date_clean'] = df['date'].apply(lambda x: conv_date(x))
    print(df)

my results:

ValueError: unconverted data remains:  12:46:21
  name                    role                 date
0   dz                  legend           2023-07-26
1   mc        sounds like a dj           26-07-2023
2   xc              loves xcom           2023/07/26
3   lz          likes a to fly           26/07/2023
4   wc     has a small bladder             26072023
5   aa  warrior of the crystal           26 07 2023
6   xx         loves only-fans  2023-07-26 12:46:21

my desired results:

  name                    role        date          date_clean
0   dz                  legend  2023-07-26          2023-07-26
1   mc        sounds like a dj  26-07-2023          2023-07-26
2   xc              loves xcom  2023/07/26          2023-07-26
3   lz          likes a to fly  26/07/2023          2023-07-26
4   wc     has a small bladder    26072023          2023-07-26
5   aa  warrior of the crystal  26 07 2023          2023-07-26
6   xx         loves only-fans  2023-07-26 12:46:21 2023-07-26
Mizanur Choudhury
  • 324
  • 2
  • 5
  • 16
  • See [this comment of mine](https://stackoverflow.com/q/76789581#comment135376398_76789581) in your previous question. – InSync Jul 28 '23 at 17:27
  • Quick question about this. When using fullmatch all my results are turned into a 2023-07-26 00:00:00 where as im looking for a 2023-07-26 format. – Mizanur Choudhury Jul 28 '23 at 17:31
  • 1
    That's because you are returning a `datetime` object, which has the "time" part. Use `.strptime().date()` to get only the date. – InSync Jul 28 '23 at 17:33
  • Amazing! Quick question. How come i cant add the .date() on my return. How come i have to add it when its assigned as a variable? `converted_date = datetime.strptime(dte, acceptable_mappings[regex]).date()` – Mizanur Choudhury Jul 28 '23 at 17:51
  • That shouldn't be the case with the code you provided in this question. Ask a new question with all the details please. – InSync Jul 28 '23 at 17:52
  • ignore me! It was my ide throwing up an error, as i hadn't changed my code to -> date, def conv_date(dte: str) -> date: Thanks for your help! – Mizanur Choudhury Jul 28 '23 at 17:56

0 Answers0