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