首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在蟒蛇大熊猫中铸造时间列和寻找时间增量

如何在蟒蛇大熊猫中铸造时间列和寻找时间增量
EN

Stack Overflow用户
提问于 2017-08-15 13:56:37
回答 1查看 8.1K关注 0票数 1

我有一个列时间,它是非空对象,我不能将它转换为timedelta或datetime。

代码语言:javascript
复制
     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()时,我得到'Time‘列是非空对象,并且无法将它转换为timedelta或datetime(为此,很明显我不能这样做)。所以,什么是找到连续的msg (时间增量)之间的区别的解决方案,但是如果是timedelta <5秒而不是pass。输出:

代码语言:javascript
复制
     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      

我试过这样的方法:

代码语言:javascript
复制
df['diff'] = (df['Time']df['Time'].shift()).fillna(0)

但我不知道写条件5秒间隔。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-15 14:00:31

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

然后得到diff和comapre与5s

最后,对于新列,请使用mask by掩码:

代码语言:javascript
复制
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
代码语言:javascript
复制
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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45694396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档