Validate time format over 24 hours with pandas

Viewed 88

I trying to find a way to validate Time format but for over 24 hours. I mean if the Time cell just count hours it can exceed 24 hours. Let's say I have this df

 Time      Family-name       Name    Sex   Birthdate
 13:34:51  Doe               John    M     1989-09-22
 32:42:02  Dolores           Trevino M     1984-02-18
 28:50:06  Emer              Han     M     1987-12-10

I tried using something like:

pd.to_datetime(df['Time'], format='%H:%M:%S', errors='coerce').notnull().all()

but of course I get errors for the times over 24 hours.

Any ideas ?

1 Answers

Use regex pattern to match:

pat = '^\d+:\d{,2}:\d{,2}$'
df['Time'].str.match(pat)

Or use pd.to_timedelta:

pd.to_timedelta(df['Time'], errors='coerce').notna()

The second approach allows '1 day 1:00:00', the first doesn't.

Related