Python-日期排序-2020.09.08

输入一组日期
利用Python判断时间是否连续
若不连续 给出间断日期 缺失了几个月份
以及缺失的日期

import pandas as pd


df = pd.read_excel(r'C:/Users/***/Desktop/**/日期排序/日期数据4.xlsx',header=None,names=['riqi'])


df.sort_values(by=['riqi'],ascending = True,inplace= True)




df['year_month'] = df['riqi'].apply(lambda x:x.strftime('%Y-%m'))   # 转换格式


df_riqi= df['year_month'].drop_duplicates().reset_index(drop = True)


blank_dic = {}  # 空值字典,从哪里开始往后缺了几个月的数据
lost_list = []  # 缺失的月份列表


for each in range(len(df_riqi)-1):     # 主要判断这个月和下个月的关系
    count = 0
    year = int(df_riqi[each].split('-')[0])
    month = int(df_riqi[each].split('-')[1])
    year_n = int(df_riqi[each+1].split('-')[0])   # 下一条记录的年份和月份
    month_n = int(df_riqi[each+1].split('-')[1])
    if month != 12 and month == (month_n -1) and year == year_n:
        pass
    elif month == 12 and month_n == 1 and year == (year_n-1):
        pass
    else:
        count = (month_n - month - 1) + (year_n - year)*12  # 统计缺失个数
        blank_dic[df_riqi[each]] = count


        while count:
            month = month + 1
            if month<=12:
                lost_list.append(str(year) + '-' + str(month))
            else:
                n = (month-1) // 12
                lost_list.append(str(year+n) + '-' + str(month-12*n))  # 大于12后,变为下一年
            count = count -1




if len(blank_dic) == 0:
    print('数据连续')
else:
    print('数据间断')
    print(blank_dic)
    print(lost_list)

1995/1/24
1995/1/26
1995/1/28
1998/3/30
1995/2/28
1995/3/2
1995/3/5
1995/6/6
1995/3/27
1995/6/25
1995/7/31
1995/8/4
1995/10/7
1995/11/8
1995/12/9
1996/6/3

out:
数据间断
{‘1995-03’: 2, ‘1995-08’: 1, ‘1995-12’: 5, ‘1996-06’: 20}
[‘1995-4’, ‘1995-5’, ‘1995-9’, ‘1996-1’, ‘1996-2’, ‘1996-3’, ‘1996-4’, ‘1996-5’, ‘1996-7’, ‘1996-8’, ‘1996-9’, ‘1996-10’, ‘1996-11’, ‘1996-12’, ‘1997-1’, ‘1997-2’, ‘1997-3’, ‘1997-4’, ‘1997-5’, ‘1997-6’, ‘1997-7’, ‘1997-8’, ‘1997-9’, ‘1997-10’, ‘1997-11’, ‘1997-12’, ‘1998-1’, ‘1998-2’]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值