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.
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