Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python groupby datetime列的运行时间(基于另一列的条件)_Python_Pandas - Fatal编程技术网

Python groupby datetime列的运行时间(基于另一列的条件)

Python groupby datetime列的运行时间(基于另一列的条件),python,pandas,Python,Pandas,我有以下数据集 lst=[ ['a','2019-03-02 23:20:28',0], ['a','2019-03-02 23:21:29',0], ['a','2019-03-02 23:22:30',1], ['a','2019-03-02 23:30:31',0.5], ['a','2019-03-02 23:38:32',0.5], ['a','2019-03-02 23:50:32',0.5], ['a','2019-03-02 23:50:32',0], ['

我有以下数据集

lst=[
 ['a','2019-03-02 23:20:28',0],
 ['a','2019-03-02 23:21:29',0],
 ['a','2019-03-02 23:22:30',1],
 ['a','2019-03-02 23:30:31',0.5],
 ['a','2019-03-02 23:38:32',0.5],
 ['a','2019-03-02 23:50:32',0.5],  
 ['a','2019-03-02 23:50:32',0], 
 ['b','2019-03-02 23:10:32',0],  
 ['b','2019-03-02 23:12:32',0],  
 ['b','2019-03-02 23:20:32',1],  
 ['b','2019-03-02 23:30:32',0.5],  
 ['b','2019-03-02 23:50:32',1],  
 ['b','2019-03-02 23:55:32',1],  
 ['b','2019-03-02 23:56:32',0],
 ['a','2019-03-02 22:20:28',0],
 ['a','2019-03-02 22:21:29',0],
 ['a','2019-03-02 22:22:30',1],
 ['a','2019-03-02 22:30:31',0.5],  
 ['a','2019-03-02 22:30:32',0], 
  ]

df = pd.DataFrame(lst,columns=['ID','ts','signal'])
df['ts']=pd.to_datetime(df['ts'])
我希望获得每个ID的所有行,即信号列中0到0之间的总运行时间大于15分钟

i、 e.仅适用于以下情况:

仅针对b:

使用:

#filter out rows with 0
df1 = df[df['signal'].ne(0)]
#create Series from original column for unique consecutive groups for non 0 rows
a = df['signal'].eq(0).cumsum()

thr = pd.Timedelta(15, unit='min')

#get difference between first and last value per group and filtering by thresh
df2 = df1[df1['ts'].groupby(a).transform(lambda x: x.iat[-1] - x.iat[0]) > thr]
print (df2)
   ID                  ts  signal
2   a 2019-03-02 23:22:30     1.0
3   a 2019-03-02 23:30:31     0.5
4   a 2019-03-02 23:38:32     0.5
5   a 2019-03-02 23:50:32     0.5
9   b 2019-03-02 23:20:32     1.0
10  b 2019-03-02 23:30:32     0.5
11  b 2019-03-02 23:50:32     1.0
12  b 2019-03-02 23:55:32     1.0

抱歉,那是打字错误。现在应该很清楚了,对于我来说,只有
pd.Timedelta(15,unit='min')
只能与
pd.Timedelta(15)
一起使用。