Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

strptime not formatting my date correctly

I was wondering if anyone smarter than me can see what i’m doing wrong.

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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{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": "xx", "role": "loves only-fans", "date": "2023-07-26 12:46:21"},
    {"name": "dz", "role": "legend", "date": "2023-07-26"},

]

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   xx  loves only-fans  2023-07-26 12:46:21
1   dz           legend           2023-07-26

my desired results:

  name             role                 date
0   xx  loves only-fans           2023-07-26
1   dz           legend           2023-07-26

>Solution :

You can first try pd.to_datetime:

df['date'] = pd.to_datetime(df['date'], format='mixed', dayfirst=False)

df['only_date'] = df['date'].dt.date
print(df)

Prints:

  name             role                date   only_date
0   xx  loves only-fans 2023-07-26 12:46:21  2023-07-26
1   dz           legend 2023-07-26 00:00:00  2023-07-26
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading