如何在熊猫时间序列中基于5分钟间隔创建组ID?

use*_*212 17 python datetime numpy pandas

我有一个时间序列数据框 df看起来像这样(时间seris发生在同一天,但在不同的时间:

                                id               val 
 time                    
2014-04-03 16:01:53             23              14389      
2014-04-03 16:01:54             28              14391             
2014-04-03 16:05:55             24              14393             
2014-04-03 16:06:25             23              14395             
2014-04-03 16:07:01             23              14395             
2014-04-03 16:10:09             23              14395             
2014-04-03 16:10:23             26              14397             
2014-04-03 16:10:57             26              14397             
2014-04-03 16:11:10             26              14397              
Run Code Online (Sandbox Code Playgroud)

我需要从开始每隔5分钟创建一个组16:00:00.即,在范围内的所有的行16:00:0016:05:00其新列的值period是1(行的每个组内的编号是不规则的,所以我不能简单地切断基团)

最终,数据应如下所示:

                                id               val           period 
time            
2014-04-03 16:01:53             23              14389             1
2014-04-03 16:01:54             28              14391             1
2014-04-03 16:05:55             24              14393             2
2014-04-03 16:06:25             23              14395             2
2014-04-03 16:07:01             23              14395             2
2014-04-03 16:10:09             23              14395             3
2014-04-03 16:10:23             26              14397             3
2014-04-03 16:10:57             26              14397             3
2014-04-03 16:11:10             26              14397             3
Run Code Online (Sandbox Code Playgroud)

目的是执行一些groupby操作,但我需要做的操作不包含在pd.resample(how=' ')方法中.所以我必须创建一个period列来识别每个组,然后做df.groupby('period').apply(myfunc).

任何帮助或评论都非常感谢.

谢谢!

Kar*_* D. 13

您可以TimeGrouper在a中使用该功能groupy/apply.使用a,TimeGrouper您无需创建期间列.我知道你不是要计算平均数,但我会用它作为一个例子:

>>> df.groupby(pd.TimeGrouper('5Min'))['val'].mean()

time
2014-04-03 16:00:00    14390.000000
2014-04-03 16:05:00    14394.333333
2014-04-03 16:10:00    14396.500000
Run Code Online (Sandbox Code Playgroud)

或者一个明确的例子apply:

>>> df.groupby(pd.TimeGrouper('5Min'))['val'].apply(lambda x: len(x) > 3)

time
2014-04-03 16:00:00    False
2014-04-03 16:05:00    False
2014-04-03 16:10:00     True
Run Code Online (Sandbox Code Playgroud)

Doctstring TimeGrouper:

Docstring for resample:class TimeGrouper@21

TimeGrouper(self, freq = 'Min', closed = None, label = None,
how = 'mean', nperiods = None, axis = 0, fill_method = None,
limit = None, loffset = None, kind = None, convention = None, base = 0,
**kwargs)

Custom groupby class for time-interval grouping

Parameters
----------
freq : pandas date offset or offset alias for identifying bin edges
closed : closed end of interval; left or right
label : interval boundary to use for labeling; left or right
nperiods : optional, integer
convention : {'start', 'end', 'e', 's'}
    If axis is PeriodIndex

Notes
-----
Use begin, end, nperiods to generate intervals that cannot be derived
directly from the associated object
Run Code Online (Sandbox Code Playgroud)

编辑

我不知道创建句点列的优雅方法,但以下内容将起作用:

>>> new = df.groupby(pd.TimeGrouper('5Min'),as_index=False).apply(lambda x: x['val'])
>>> df['period'] = new.index.get_level_values(0)
>>> df

                     id    val  period
time
2014-04-03 16:01:53  23  14389       0
2014-04-03 16:01:54  28  14391       0 
2014-04-03 16:05:55  24  14393       1
2014-04-03 16:06:25  23  14395       1
2014-04-03 16:07:01  23  14395       1
2014-04-03 16:10:09  23  14395       2
2014-04-03 16:10:23  26  14397       2
2014-04-03 16:10:57  26  14397       2
2014-04-03 16:11:10  26  14397       2
Run Code Online (Sandbox Code Playgroud)

它的工作原理是因为这里的group_ as_index = False实际上返回了你想要的周期列作为多索引的一部分,我只是抓住多索引的那一部分并分配给原始数据帧中的一个新列.您可以在apply中执行任何操作,我只想要索引:

>>> new

   time
0  2014-04-03 16:01:53    14389
   2014-04-03 16:01:54    14391
1  2014-04-03 16:05:55    14393
   2014-04-03 16:06:25    14395
   2014-04-03 16:07:01    14395
2  2014-04-03 16:10:09    14395
   2014-04-03 16:10:23    14397
   2014-04-03 16:10:57    14397
   2014-04-03 16:11:10    14397

>>>  new.index.get_level_values(0)

Int64Index([0, 0, 1, 1, 1, 2, 2, 2, 2], dtype='int64')
Run Code Online (Sandbox Code Playgroud)

  • `pd.TimeGrouper` 已被[废弃](/sf/answers/3160941101/)。现在使用“pd.Grouper”。 (3认同)