Ad

Remove Rows With Invalid Date And Time Data

below is my sample dataframe.

enter image description here

I need to remove the columns that have an invalid date or time. Therefore rows 3,5,6 should be deleted. I need a way to verify if the date & time columns contain valid date & times.

Ad

Answer

IIUC, you could use to_datetime method with errors=coerce parameter; then use notna to create a boolean mask to filter df with:

df = df[pd.to_datetime(df['time'], errors='coerce').notna()]
Ad
source: stackoverflow.com
Ad