Querying timedelta column in pandas, and filtering rows

泄露秘密 提交于 2019-12-01 21:56:24

You can convert timedeltas 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!