[英]In python, how do I create a list of lists where each list contains a string representation of a start and end date?
My desired output would be this:我想要的 output 是这样的:
date_list = [
['2021-04-15', '2021-04-20'],
['2021-04-21', '2021-04-26'],
['2021-04-27', '2021-05-02'],
['2021-05-03', '2021-05-08'],
['2021-05-09', '2021-05-16'],
]
I would like to be able to define a start_date ('2021-04-15'), an end_date ('2021-05-16'), and an integer representing how many days I would like in each chunk (for example, I want each sub-list to contain a 5-day chunk).我希望能够定义一个 start_date ('2021-04-15')、一个 end_date ('2021-05-16') 和一个 integer 代表我希望在每个块中的天数(例如,我希望每个子列表包含一个 5 天的块)。 I also need to make sure that the end_date is included in the last list/chunk regardless of the size of that chunk.
我还需要确保 end_date 包含在最后一个列表/块中,无论该块的大小如何。 I need to be able to use the elements of each list to parameterize a SQL loop and have attempted many solutions and none have been even close to what I am looking for.
我需要能够使用每个列表的元素来参数化 SQL 循环并尝试了许多解决方案,但没有一个与我正在寻找的解决方案相近。
>>> import datetime
>>> start = datetime.datetime(year=2021, month=4, day=15)
>>> end = datetime.datetime(year=2021, month=5, day=16)
>>> delta = datetime.timedelta(days=5)
>>> result = []
>>> while start <= end:
result.append([start.strftime('%Y-%m-%d'), (start+delta).strftime('%Y-%m-%d')])
start += delta + datetime.timedelta(days=1)
>>> result
[['2021-04-15', '2021-04-20'],
['2021-04-21', '2021-04-26'],
['2021-04-27', '2021-05-02'],
['2021-05-03', '2021-05-08'],
['2021-05-09', '2021-05-14'],
['2021-05-15', '2021-05-20']]
This will work:这将起作用:
import datetime
import pandas as pd
def get_date_chunks(start_date, end_date, chunk_size):
curr = datetime.datetime.strptime(start_date, "%Y-%m-%d")
end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d")
offset = pd.DateOffset(days=chunk_size)
result = []
while curr <= end_date:
result.append([str(curr.date()), str((curr + offset).date())])
curr += pd.DateOffset(days = chunk_size + 1)
# Make sure last date matches up with the end_date
if datetime.datetime.strptime(result[-1][0], "%Y-%m-%d") > end_date:
result.pop()
else:
result[-1][1] = str(end_date.date())
return result
get_date_chunks('2021-04-15', '2021-05-16', 5)
This line:这一行:
if datetime.datetime.strptime(result[-1][0], "%Y-%m-%d") > end_date:
Means that your final list could be ['2021-05-15', '2021-05-15']
, which guarentees that you will always include the end date, but you will never see a day beyond your end date.意味着您的最终列表可能是
['2021-05-15', '2021-05-15']
,这保证您将始终包含结束日期,但您永远不会看到结束日期之后的一天。
This one does it exactly as you specified in your desired output.这个完全按照您在所需的 output 中指定的方式进行。 If the last chunk would be smaller than 5, it simply unites it with the previous one.
如果最后一个块小于 5,它只是将它与前一个块合并。
import datetime
from dateutil import parser
def create_date_list(start_date, end_date, chunk_size):
start_date = parser.parse(start_date)
end_date = parser.parse(end_date)
dates = []
i = 0
while True:
chunk_start = start_date + i*datetime.timedelta(chunk_size) + datetime.timedelta(i)
chunk_end = chunk_start + datetime.timedelta(chunk_size)
if (end_date - chunk_end).days <= chunk_size:
dates.append([str(chunk_start).split()[0], str(end_date).split()[0]])
break
else:
dates.append([str(chunk_start).split()[0], str(chunk_end).split()[0]])
i += 1
return dates
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.