We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Cleaning Dates

Dates are messy because everyone writes them differently. 2024-01-15, 01/15/2024, and Jan 15, 2024 are all the same day, but completely different strings.

The pd.to_datetime() function can convert dates into a single datetime64[ns] type. Pass format="mixed" when the input contains multiple formats. It can also handle invalid dates with the errors="coerce" option, which turns them into NaT ("Not a Time").

# Parse mixed formats
df["date"] = pd.to_datetime(df["date"], format="mixed")

# Coerce invalid values to NaT
df["date"] = pd.to_datetime(df["date"], format="mixed", errors="coerce")

Assignment

Complete the parse_device_timestamps function. It accepts a DataFrame with a timestamp (string) column, then returns a DataFrame with parsed datetimes and invalid timestamp rows removed.