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

如何自动生成跨年度分旬场景下指定格式的日期序列?

问题背景

我有一份软件生成的结果文件,内容如下:

,0,1,2,3,4,5,6,7,8,9
0,Month,Decade,Stage,Kc,ETc,ETc,Eff,rain,Irr.,Req.
1,coeff,mm/day,mm/dec,mm/dec,mm/dec,,,,,
2,Sep,1,Init,0.50,1.85,18.5,21.8,0.0,,
3,Sep,2,Init,0.50,1.77,17.7,30.3,0.0,,
4,Sep,3,Init,0.50,1.72,17.2,37.1,0.0,,
5,Oct,1,Deve,0.61,2.05,20.5,49.5,0.0,,
6,Oct,2,Deve,0.82,2.66,26.6,59.3,0.0,,
7,Oct,3,Deve,1.03,3.24,35.6,43.0,0.0,,
8,Nov,1,Mid,1.20,3.63,36.3,20.9,15.4,,
9,Nov,2,Mid,1.21,3.53,35.3,6.0,29.2,,
10,Nov,3,Mid,1.21,3.70,37.0,4.0,33.0,,
11,Dec,1,Mid,1.21,3.87,38.7,0.1,38.6,,
12,Dec,2,Late,1.18,3.92,39.2,0.0,39.2,,
13,Dec,3,Late,1.00,3.58,39.4,0.0,39.4,,
14,Jan,1,Late,0.88,3.36,10.1,0.0,10.1,,
15,,,,,,,,,,
16,372.1,272.2,204.9,,,,,,,

数据的月份范围从9月到次年1月,每个月被划分为3个旬(Decade),具体时间跨度为2017年9月到2018年1月上旬。需要生成每个月对应旬的起始日期,格式为01-Sep-2017,预期结果序列为01-Sep-2017、11-Sep-2017、21-Sep-2017……直到01-Jan-2018。

现有代码如下:

years = [2017, 2018, 2019]
temp = pd.read_csv(folder_link) # Reading the particular result file
Month = temp['0'][2:] # First column = Month (Jul, Aug, ..)
Decade = temp['1'][2:]
for year in years:
    for j in range(2,len(Decade)): # First two lines are headers, so removed them
        if(int(Decade[j]) == 1): # First decade = 1-10 days of month
            Date = "1" + "-" + Month[j] + "-" + str(year) # Writing the date as 1-Jan-2017
            Dates.append(Date)
        if(int(Decade[j]) == 2): # Second decade = 11-20 days of month
            Date = "11" + "-" + Month[j] + "-" + str(year)
            Dates.append(Date)  
        if(int(Decade[j]) == 3): # Third decade = 21-28 or 21-30 or 21-31 days of month
            Date = "21" + "-" + Month[j] + "-" + str(year)
            Dates.append(Date)

当前代码的问题是生成1月的日期时会错误输出为01-Jan-2017而非正确的2018年。需要一套通用解决方案,可以适配所有跨年度的场景,比如还有部分数据覆盖2017年9月到2018年8月的时间范围。

解决方案

核心逻辑是判断月份是否出现了环比减小的情况,只要当前月份的数字比前一个月小,就说明进入了新的年度,自动给年份加1即可,适配任意跨年度的时间区间。

实现代码

import pandas as pd
from datetime import datetime

# 配置基础参数
start_year = 2017  # 数据起始年份,根据实际情况修改即可
folder_link = "你的文件路径"
Dates = []

# 读取数据并过滤有效行
temp = pd.read_csv(folder_link)
valid_df = temp.loc[2:14, ['0', '1']]  # 取2-14行的有效数据,可根据实际文件调整
valid_df.columns = ['month_str', 'decade']

# 初始化年份变量
current_year = start_year
# 记录上一个月的数字,初始设为None
prev_month_num = None

for _, row in valid_df.iterrows():
    month_str = row['month_str']
    decade = int(row['decade'])
    
    # 把月份缩写转成数字,比如Sep转成9,Jan转成1
    current_month_num = datetime.strptime(month_str, '%b').month
    
    # 核心判断:如果当前月份比上一个小,说明跨年了,年份+1
    if prev_month_num is not None and current_month_num < prev_month_num:
        current_year += 1
    
    # 生成对应日期
    if decade == 1:
        day = '01'
    elif decade == 2:
        day = '11'
    elif decade == 3:
        day = '21'
    
    date_str = f"{day}-{month_str}-{current_year}"
    Dates.append(date_str)
    
    # 更新上一个月的数字
    prev_month_num = current_month_num

方案优势

  • 不需要提前预设所有年份,只需要填写起始年份即可自动处理跨年场景
  • 适配任意跨年度的时间区间,不管是跨1年还是多年,只要月份是按顺序排列的都可以正确生成日期
  • 日期格式统一为两位天,符合要求的01-Sep-2017格式,不会出现单数字天的情况

内容的提问来源于stack exchange,提问作者G.S. J

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 22:24:03