我正在试着分组消息,这些消息很快就被发送出go 了.参数定义消息之间的最长持续时间,以便它们被视为块的一部分.如果将消息添加到块中,则时间窗口将延长,以便更多消息被视为块的一部分.

Example Input

datetime message
0 2023-01-01 12:00:00 A
1 2023-01-01 12:20:00 B
2 2023-01-01 12:30:00 C
3 2023-01-01 12:30:55 D
4 2023-01-01 12:31:20 E
5 2023-01-01 15:00:00 F
6 2023-01-01 15:30:30 G
7 2023-01-01 15:30:55 H

Expected output for the parameter set to 1min

datetime message datetime_last n_block
0 2023-01-01 12:00:00 A 2023-01-01 12:00:00 1
1 2023-01-01 12:20:00 B 2023-01-01 12:20:00 1
2 2023-01-01 12:30:00 C\nD\nE 2023-01-01 12:31:20 3
3 2023-01-01 15:00:00 F 2023-01-01 15:00:00 1
4 2023-01-01 15:30:30 G\nH 2023-01-01 15:30:55 2

My failing attempt

我希望通过滚动窗口来实现这一点,该窗口将不断地追加消息行.

def join_messages(x):
    return '\n'.join(x)

df.rolling(window='1min', on='datetime').agg({
  'datetime': ['first', 'last'], 
  'message': [join_messages, "count"]}) #Somehow overwrite datetime with the aggregated datetime.first.

两个聚合都失败,返回ValueError:invalid on specified as datetime, must be a column (of DataFrame), an Index or None.

我看不出有什么干净利落的方法可以让datetime在橱窗里变得"容易接近".此外,滚动也不能很好地与弦配合使用.我的印象是,这是一条死胡同,有一种更干净的方法来解决这一问题.

输入和预期数据的片段

df = pd.DataFrame({
    'datetime': [pd.Timestamp('2023-01-01 12:00'),
                 pd.Timestamp('2023-01-01 12:20'),
                 pd.Timestamp('2023-01-01 12:30:00'),
                 pd.Timestamp('2023-01-01 12:30:55'),
                 pd.Timestamp('2023-01-01 12:31:20'),
                 pd.Timestamp('2023-01-01 15:00'),
                 pd.Timestamp('2023-01-01 15:30:30'),
                 pd.Timestamp('2023-01-01 15:30:55'),],
    'message': list('ABCDEFGH')})


df_expected = pd.DataFrame({
    'datetime': [pd.Timestamp('2023-01-01 12:00'),
                 pd.Timestamp('2023-01-01 12:20'),
                 pd.Timestamp('2023-01-01 12:30:00'),
                 pd.Timestamp('2023-01-01 15:00'),
                 pd.Timestamp('2023-01-01 15:30:30'),],
    'message': ['A', 'B', 'C\nD\nE', 'F', 'G\nH'],
    'datetime_last': [pd.Timestamp('2023-01-01 12:00'),
                      pd.Timestamp('2023-01-01 12:20'),
                      pd.Timestamp('2023-01-01 12:31:20'),
                      pd.Timestamp('2023-01-01 15:00'),
                      pd.Timestamp('2023-01-01 15:30:55'),],
    'n_block': [1, 1, 3, 1, 2]})

推荐答案

比较当前和以前的日期时间值以标记差异大于1分钟的行,然后在该标志上应用累积和以区分不同的日期时间块.现在,按这些数据块对数据帧进行分组并聚合以获得结果

m = df['datetime'].diff() > pd.Timedelta(minutes=1)
df.groupby(m.cumsum(), as_index=False).agg(datetime=('datetime', 'first'),
                                           datetime_last=('datetime', 'last'),
                                           message=('message', '\n'.join),
                                           n_block=('message', 'count'))

             datetime       datetime_last  message  n_block
0 2023-01-01 12:00:00 2023-01-01 12:00:00        A        1
1 2023-01-01 12:20:00 2023-01-01 12:20:00        B        1
2 2023-01-01 12:30:00 2023-01-01 12:31:20  C\nD\nE        3
3 2023-01-01 15:00:00 2023-01-01 15:00:00        F        1
4 2023-01-01 15:30:30 2023-01-01 15:30:55     G\nH        2

Python相关问答推荐

有没有方法可以关闭Python多处理资源跟踪器进程?

当测试字符串100%包含查询字符串时,为什么t fuzzywuzzy s Process.extractBests不给出100%分数?

使用Python Great Expectations和python-oracledb

在Python中使用一行try

在Python中管理多个OpenGVBO和VAO实例

如何使用Python中的clinicalTrials.gov API获取完整结果?

Python plt.text中重叠,包adjust_text不起作用,如何修复?

使用pandas、matplotlib和Yearbox绘制时显示错误的年份

如何计算列表列行之间的公共元素

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

Django管理面板显示字段最大长度而不是字段名称

Python上的Instagram API:缺少client_id参数"

为什么以这种方式调用pd.ExcelWriter会创建无效的文件格式或扩展名?

PyQt5,如何使每个对象的 colored颜色 不同?'

Python+线程\TrocessPoolExecutor

在含噪声的3D点网格中识别4连通点模式

如何在turtle中不使用write()来绘制填充字母(例如OEG)

索引到 torch 张量,沿轴具有可变长度索引

如何在Python中获取`Genericums`超级类型?

网格基于1.Y轴与2.x轴显示在matplotlib中