You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas DataFrame时间索引去重:解决微秒级时间重叠问题

解决股票Tick数据Datetime索引重复与重叠问题

在处理股票Tick数据的Pandas DataFrame时,常会遇到两类Datetime索引问题:

  • 同一Id分组内多条记录的时间完全相同
  • 相邻Id分组的时间仅相差1微秒

如果直接用groupby+cumcount给组内记录添加微秒增量,会出现相邻组时间重叠的情况,无法生成全局严格递增无重复的时间索引。以下提供两种适配百万级数据量的解决方案:

方案一:高效数组版(推荐大数据量)

利用Pandas向量化操作实现,避免循环,性能更优:

import pandas as pd
import numpy as np

# 假设原始数据df包含'id'列和'datetime'列,先按id和时间排序
df = df.sort_values(['id', 'datetime'])

# 1. 计算每个组内的累计序列(从0开始)
df['group_seq'] = df.groupby('id').cumcount()

# 2. 提取每个组的最小时间,同时获取前一组的最大时间
group_stats = df.groupby('id')['datetime'].agg(['min', 'max']).reset_index()
group_stats['prev_group_max'] = group_stats['max'].shift(1)

# 3. 计算每个组的起始偏移:确保当前组起始时间严格大于前一组最大时间
group_stats['offset'] = np.where(
    group_stats['prev_group_max'] >= group_stats['min'],
    (group_stats['prev_group_max'] - group_stats['min']).dt.microseconds + 1,
    0
)

# 4. 将偏移量映射回原表,计算最终时间
df = df.merge(group_stats[['id', 'offset']], on='id')
df['final_datetime'] = df['datetime'] + pd.to_timedelta(df['offset'] + df['group_seq'], unit='us')

# 设置为索引并排序
df = df.set_index('final_datetime').sort_index()

方案二:循环版(逻辑直观)

逐组处理,跟踪上一组的最后时间,确保当前组时间严格大于上一组:

import pandas as pd

# 先按id和时间排序原始数据
df = df.sort_values(['id', 'datetime'])
groups = df.groupby('id')
final_times = []
last_group_end = None

for _, group in groups:
    # 取当前组的基准时间
    base_time = group['datetime'].iloc[0]
    # 若上一组的结束时间大于等于当前组基准时间,调整基准时间
    if last_group_end is not None and last_group_end >= base_time:
        base_time = last_group_end + pd.Timedelta(1, unit='us')
    # 给组内每条记录分配递增的微秒时间
    group_timestamps = base_time + pd.to_timedelta(range(len(group)), unit='us')
    final_times.extend(group_timestamps)
    # 更新上一组的结束时间
    last_group_end = group_timestamps.iloc[-1]

# 赋值并设置索引
df['final_datetime'] = final_times
df = df.set_index('final_datetime').sort_index()

两种方案都能确保最终的Datetime索引全局严格递增、无重复,其中数组版更适合百万级以上的大样本数据,循环版逻辑更易理解调试。

内容的提问来源于stack exchange,提问作者Dutchy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 10:32:36