python - 如何根据 Pandas 时间序列中的 5 分钟间隔创建组 ID?

标签 python datetime numpy pandas

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

                                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              

我需要从 16:00:00 开始每 5 分钟创建一次组。即所有在 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

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

非常感谢任何帮助或评论。

谢谢!

最佳答案

您可以在groupy/apply 中使用TimeGrouper 函数。使用 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

或者一个带有显式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

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

编辑

我不知道有什么优雅的方法可以创建句点列,但以下方法可行:

>>> 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

之所以有效,是因为此处 as_index=False 的 groupby 实际上返回了您想要的周期列作为多索引的一部分,我只是捕获了多索引的那部分并分配给原始数据框中的新列。你可以在申请中做任何事情,我只想要索引:

>>> 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')

关于python - 如何根据 Pandas 时间序列中的 5 分钟间隔创建组 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966152/

相关文章:

python - 如何在python中模拟随机游走的首次通过时间概率?

python - MySQL:如果不重新打开 shell,Django .create 后跟 .get 将无法工作

python - sqlite3 python2.7 和 Ubuntu 10.04(或只是 python2.6)

python - pandas:使用不带引号的文字制表符编写制表符分隔的数据框

python - 对不同大小的顺序数组切片进行操作

python - 压缩格式 block 对角矩阵的高效线性代数

python - 带有主机谓词的 python 脚本中的 Mongorestore

jsf - 使用 <f :convertDateTime> with <h:commandLink>

sql - 用HQL计算两个字段之间的差异范围

java - 如何将事件的开始时间和结束时间与其他事件的开始时间和结束时间进行比较