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

基于Pandas按类别在DataFrame末尾添加连续日期行的问题

解决DataFrame按Area类别添加连续日期行的日期偏移问题

问题背景

需要基于Area列,在DataFrame每个类别的末尾添加一行连续日期的记录,但现有代码的日期偏移逻辑无法生成符合预期的结果。

原数据

DateStartEndAreaIDStat
1/1/20222/1/20223/1/2022NY222Y
2/1/20223/1/20224/1/2022NY111Y
1/1/20222/1/20223/1/2022CA333Y
2/1/20223/1/20224/1/2022CA100Y

期望结果

DateStartEndAreaIDStat
1/1/20222/1/20223/1/2022NY222Y
2/1/20223/1/20224/1/2022NY111Y
3/1/20224/1/20225/1/2022NY
1/1/20222/1/20223/1/2022CA333Y
2/1/20223/1/20224/1/2022CA100Y
3/1/20224/1/20225/1/2022CA

现有问题代码

# Convert the cols to datetime
c = ['Start', 'End']
df[c] = df[c].apply(pd.to_datetime, dayfirst=True)

# drop the duplicates rows by Area while keeping only the last row
rows = df[[*c, 'Area']].drop_duplicates('Area', keep='last')

# Add a dateoffset of 1 day
rows[c] += pd.DateOffset(days=1)

# Concat the rows and sort index to maintain order
pd.concat([df, rows]).sort_index(ignore_index=True)

问题分析与修复方案

现有代码的核心问题:

  • 遗漏了Date列的偏移处理,导致新增行的Date缺失
  • 日期偏移逻辑不符合预期:直接对Start/End加1天,没有承接原最后一行的End值生成连续序列

修复后的完整代码:

import pandas as pd

# 转换日期列为datetime格式,匹配原数据的MM/DD/YYYY格式
date_cols = ['Date', 'Start', 'End']
df[date_cols] = df[date_cols].apply(pd.to_datetime, format='%m/%d/%Y')

# 按Area分组,获取每个区域的最后一条记录
last_records = df.groupby('Area').last().reset_index()

# 生成需要添加的新行
new_records = last_records.copy()
# 新行的Date、Start承接原最后一行的End值
new_records['Date'] = last_records['End']
new_records['Start'] = last_records['End']
# 新行的End在原最后一行End基础上加1天
new_records['End'] = last_records['End'] + pd.DateOffset(days=1)
# 清空ID和Stat列
new_records[['ID', 'Stat']] = pd.NA

# 合并原数据与新行,按Area和Date排序保证顺序
final_df = pd.concat([df, new_records]).sort_values(['Area', 'Date'], ignore_index=True)

# 可选:转换回原日期字符串格式
final_df[date_cols] = final_df[date_cols].dt.strftime('%m/%d/%Y')
print(final_df)

关键修复点

  1. 日期解析修正:用format='%m/%d/%Y'精准匹配原数据的日期格式,避免dayfirst=True可能导致的解析错误
  2. 精准取最后一行:通过groupby('Area').last()替代drop_duplicates,确保每个区域的最后一条记录被正确提取
  3. 符合预期的日期逻辑:严格对齐期望结果,新行的Date和Start等于原区域最后一行的End,End再顺延1天
  4. 格式对齐:清空新增行的ID和Stat列,保证与期望结果一致
  5. 排序保证顺序:合并后按Area和Date排序,确保每个区域的记录按日期顺序排列

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 07:20:11