本文介绍了如何在Python Pandas中强制转换时间列并查找有条件的timedelta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Time列,该列不是空对象,并且无法将其转换为timedelta或datetime.

I have a column Time which is non null object and I cannot convert it to timedelta or datetime.

     Time             msg
12:29:36.306000      Setup
12:29:36.507000      Alerting
12:29:38.207000      Service
12:29:39.194000      Setup
12:30:05.773000      Alerting
12:30:06.205000      Service
12:32:07.315000      Setup
12:32:17.194000      Service
12:32:26.889000      Setup
12:36:06.274000      Alerting
12:36:08.523000      Service
12:37:59.200000      Setup
12:47:10.652000      Alerting
12:47:43.921000      Setup

当我输入df.info()时,我发现时间"列不是非空对象,因此无法将其转换为timedelta或datetime(为此,很明显为什么我不能做到这一点).因此,找到连续的msg(时间增量)之间的差异的解决方案是什么,但是如果timedelta <比通过5秒.输出:

When I type df.info(), I got that a 'Time' column is non null object and I couldn't convert it to timedelta or datetime(for this it's obvious why I can't do it). So, what's is the solution to find the difference between consecutive msg (time delta), but if is timedelta < 5sec than pass.Output:

     Time             msg         diff
12:29:36.306000      Setup         
12:29:36.507000      Alerting      
12:29:38.207000      Service
12:29:39.194000      Setup
12:30:05.773000      Alerting
12:30:06.205000      Service
12:32:07.315000      Setup
12:32:17.194000      Service
12:32:26.889000      Setup
12:36:06.274000      Alerting    6.30***
12:36:08.523000      Service     
12:37:59.200000      Setup
12:47:10.652000      Alerting    11.02***    
12:47:43.921000      Setup      

我已经尝试过这样的事情:

I've tried with something like this:

df['diff'] = (df['Time']df['Time'].shift()).fillna(0)

但是我不知道在5秒间隔内写条件.

But I didn't know to write condition for 5sec interval.

推荐答案

我认为首先需要转换为str,然后调用 to_timedelta .

I think first need convert to str and then call to_timedelta.

然后获取 diff 并与5s进行比较.

最后一个新列使用 mask 通过遮罩:

Last for new column use mask by mask:

df['Time'] = pd.to_timedelta(df['Time'].astype(str))

df['diff'] = df['Time'].diff()
df['mask'] = df['Time'].diff() > pd.Timedelta(5, unit='s')
print (df)
              Time       msg            diff   mask
0  12:29:36.306000     Setup             NaT  False
1  12:29:36.507000  Alerting 00:00:00.201000  False
2  12:29:38.207000   Service 00:00:01.700000  False
3  12:29:39.194000     Setup 00:00:00.987000  False
4  12:30:05.773000  Alerting 00:00:26.579000   True
5  12:30:06.205000   Service 00:00:00.432000  False
6  12:32:07.315000     Setup 00:02:01.110000   True
7  12:32:17.194000   Service 00:00:09.879000   True
8  12:32:26.889000     Setup 00:00:09.695000   True
9  12:36:06.274000  Alerting 00:03:39.385000   True
10 12:36:08.523000   Service 00:00:02.249000  False
11 12:37:59.200000     Setup 00:01:50.677000   True
12 12:47:10.652000  Alerting 00:09:11.452000   True
13 12:47:43.921000     Setup 00:00:33.269000   True


df['Time'] = pd.to_timedelta(df['Time'])
diff = df['Time'].diff()
mask = df['Time'].diff() > pd.Timedelta(5, unit='s')
df['new'] = diff.where(mask)
print (df)
              Time       msg             new
0  12:29:36.306000     Setup             NaT
1  12:29:36.507000  Alerting             NaT
2  12:29:38.207000   Service             NaT
3  12:29:39.194000     Setup             NaT
4  12:30:05.773000  Alerting 00:00:26.579000
5  12:30:06.205000   Service             NaT
6  12:32:07.315000     Setup 00:02:01.110000
7  12:32:17.194000   Service 00:00:09.879000
8  12:32:26.889000     Setup 00:00:09.695000
9  12:36:06.274000  Alerting 00:03:39.385000
10 12:36:08.523000   Service             NaT
11 12:37:59.200000     Setup 00:01:50.677000
12 12:47:10.652000  Alerting 00:09:11.452000
13 12:47:43.921000     Setup 00:00:33.269000

这篇关于如何在Python Pandas中强制转换时间列并查找有条件的timedelta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:34