简体   繁体   English

如何从开始日期和结束日期列表中识别丢失的日期?

[英]How to identify missing days from a list of start and end dates?

How to identify missing dates from a list of (start_date, end_date) tuples? 如何从(start_date, end_date)元组列表中识别缺少的日期?

For example, how to identify missing dates in the below list: 例如,如何在下面的列表中标识缺少的日期:

dates = [('2011-01-01', '2011-01-14'), ('2011-01-15','2011-01-31'), 
         ('2011-02-01', '2011-02-14'), ('2011-03-01', '2011-03-14'), 
         ('2011-03-16', '2011-03-31')]

The above example has the following missing dates: 上面的示例缺少以下日期:

  • 2011-02-15 to 2011-02-28 2011-02-152011-02-28
  • 2011-03-15

How do identify all missing days from a list of start and end date tuples? 如何从开始和结束日期元组列表中识别所有丢失的日期?

Solution in more object-oriented fashion: 以更多面向对象的方式解决方案:

from functools import total_ordering
from datetime import timedelta, datetime


@total_ordering
class DateRange(object):
    def __init__(self, start, end):
        assert start <= end
        self.start = start
        self.end = end

    def __contains__(self, other):
        return self.start <= other and self.end >= other

    def __lt__(self, other):
        if self.start == other.start:
            return self.end < other.end
        return self.start < other.start

    def __eq__(self, other):
        return self.start == other.start and self.end == other.end

    def __str__(self):
        return '<%s, %s>' % (self.start.strftime('%Y-%m-%d'), self.end.strftime('%Y-%m-%d'))

    def __iter__(self):
        class DateIterator(object):
            def __init__(self, start, end):
                self.current = start
                self.end = end

            def next(self):
                if self.current > self.end:
                    raise StopIteration()
                self.current += timedelta(days=1)
                return self.current

        return DateIterator(self.start, self.end)

    __repr__ = __str__

dates = [('2011-01-01', '2011-01-14'), ('2011-01-15','2011-01-31'), ('2011-02-01','2011-02-14'), ('2011-03-01','2011-03-14'), ('2011-03-16','2011-03-31')]
dates = [DateRange(datetime.strptime(start, '%Y-%m-%d'), datetime.strptime(end, '%Y-%m-%d')) for start, end in dates]
dates = sorted(dates)

missing = []

previous = None
for date_range in dates:
    if previous is not None and previous < date_range.start:
        missing.append(DateRange(previous, date_range.start + timedelta(days=-1)))

    previous = date_range.end + timedelta(days=1)

print missing

A bit verbose, but you get the idea 有点冗长,但您明白了

EDIT: cleaned it up a bit 编辑:清理了一下

from datetime import date, timedelta

dates = [('2011-01-01', '2011-01-14'), ('2011-01-15','2011-01-31'), 
         ('2011-02-01', '2011-02-14'), ('2011-03-01', '2011-03-14'), 
         ('2011-03-16', '2011-03-31')]

def d_range(d1,d2):
    delta = d2 - d1 #assumes second date is always after first
    return [d1 + timedelta(days=i) for i in range(delta.days + 1)]

my_days = []

#calc the date range between each tuple
for d in dates:

    d1 = datetime.strptime(d[0],'%Y-%m-%d')
    d2 = datetime.strptime(d[1],'%Y-%m-%d')

    my_days.extend(d_range(d1,d2))


#now do the same for the max and min dates
my_range = d_range(min(my_days), max(my_days))


missing_dates = set(my_range).difference(set(my_days))

And to address your comment: 并发表您的评论:

missing_dates_list = list(missing_dates)

or back to thier original format: 或返回原始格式:

missing_dates_str =  [datetime.strftime(date, '%Y-%m-%d') for date in missing_dates]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python。 如何从日期列表中获取开始和结束日期 - Python. How to get start and end date from list of dates 如何在 Python 中从开始日期和结束日期创建日期列表? - How can I create a list of dates from start and end dates in Python? 我的日期列表中缺少日期。 如何运行 function 以在缺失的行中添加自定义日期? - Days are missing in my list of dates. How can run a function to add custom days in missing rows? 如何在Python列表中标识序列的开始和结束? - How to identify the start and the end of a sequence inside a list in Python? 用介于两者之间的日期填充开始/结束日期列表 - Fill list of start/end dates with dates in between 从给定的开始/结束日期列表创建“PeriodIndex” - Create `PeriodIndex` from given list of start/end dates 如何从 csv 获取日期列表(作为字符串)并仅返回开始日期和结束日期之间的日期/数据? - How can I take list of Dates from csv (as strings) and return only the dates/data between a start date and end date? 如何根据开始和结束获取for循环中的日期列表? - How to get list of dates in for loop based on start and end? 给定开始日期和结束日期的数据框,如何计算一年中每一天的总入住天数? - How to calculate total occupancy days for each day of year, given a dataframe of start and end dates? 如何从Bokeh的x轴开始和结束中删除日期? - How to remove dates from start and end of the x-axis in Bokeh?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM