I have a column of timedelta in pandas. It is in the format x days 00:00:00. I want to filter out and flag the rows which have a value >=30 minutes. I have no clue how to do that using pandas. I tried booleans and if statements but it didn't work. Any help would be appreciated.
Asked
Active
Viewed 8,748 times
1 Answers
16
You can convert timedelta
s to seconds by total_seconds
and compare with scalar:
df = df[df['col'].dt.total_seconds() < 30]
Or compare with Timedelta
:
df = df[df['col'] < pd.Timedelta(30, unit='s')]
Sample:
df = pd.DataFrame({'col':pd.to_timedelta(['25:10:01','00:01:20','00:00:20'])})
print (df)
col
0 1 days 01:10:01
1 0 days 00:01:20
2 0 days 00:00:20
df = df[df['col'].dt.total_seconds() < 30]
print (df)
col
2 00:00:20

jezrael
- 822,522
- 95
- 1,334
- 1,252