The issue is that the format you provided does not match the format of the second date type you mentioned. You should update the format string to match the actual format of the dates in the column. Following is the updated code to handle both date formats:
import pandas as pd
# Sample data
data = {'date': ['3/4/2023 4:02:55 PM', '2023-03-04-15.22.31.000000']}
df = pd.DataFrame(data)
# Custom parser function
def custom_parser(date_str):
for fmt in ['%d/%m/%Y %I:%M:%S %p', '%Y-%m-%d-%H.%M.%S.%f']:
try:
return pd.to_datetime(date_str, format=fmt)
except ValueError:
pass
return pd.NaT
df['date'] = df['date'].apply(custom_parser)
print(df)
In this code, I created a custom parser function custom_parser
that tries to parse the date string with multiple formats. If none of the formats work, it returns pd.NaT
(Not a Time) to represent a missing value. Then, I used the apply
function to apply the custom parser to each date string in the 'date' column.
Now the 'date' column should have the correct datetime values for both formats without any null values.