I have below code which make a list of date between start and end date with a frequency of 13 days. I'm not sure how I can make it a list with start and end date on it(as what is shown in my expected output)
Note: I should also added those remaining dates
Python Code
from datetime import datetime, timedelta
import pandas as pd
start_date = '2021-10-01T17:30:00Z'
end_date = '2021-10-30T21:00:00Z'
pd.date_range(start_date, end_date ,freq='13D')
Output:
DatetimeIndex(['2021-10-01 17:30:00 00:00', '2021-10-14 17:30:00 00:00','2021-10-27 17:30:00 00:00'],
Expected output:
[
{
"start_date": "2021-10-01 17:30:00 00:00",
"end_date":"2021-10-14 17:30:00 00:00"
},
{
"start_date": "2021-10-14 17:30:00 00:00",
"end_date":"2021-10-27 17:30:00 00:00"
},
{
"start_date": "2021-10-27 17:30:00 00:00",
"end_date":"2021-10-30 17:30:00 00:00"
}
]
CodePudding user response:
You can use comprehensions to solve this problem (I'm assuming you might want to extend this to more than 3 dates in the future, otherwise you could just write out the expected output by hand and be done with it):
from datetime import datetime, timedelta
import pandas as pd
start_date = '2021-10-01T17:30:00Z'
end_date = '2021-10-30T21:00:00Z'
date_range = pd.date_range(start_date, end_date, freq='13D')
intervals = [{
"start_date": str(date_range[i]),
"end_date": str(date_range[i 1])
} for i in range(len(date_range) - 1)]
if intervals and pd.Timestamp(end_date) != intervals[-1]["end_date"]:
intervals.append({
"start_date": intervals[-1]["end_date"],
"end_date": end_date
})
print(intervals)
CodePudding user response:
Try this:
In [382]: x = pd.date_range(start_date, end_date ,freq='13D')
In [389]: l = []
In [393]: for ix, ele in enumerate(x):
...: if ix 1 < len(x):
...: d = {"start_date": str(ele), "end_date": str(x[ix 1])}
...: l.append(d)
...:
In [400]: if l[-1]['end_date'] < end_date:
...: l.append({'start_date': l[-1]['end_date'], 'end_date':end_date})
...:
In [401]: l
Out[401]:
[{'start_date': '2021-10-01 17:30:00 00:00',
'end_date': '2021-10-14 17:30:00 00:00'},
{'start_date': '2021-10-14 17:30:00 00:00',
'end_date': '2021-10-27 17:30:00 00:00'},
{'start_date': '2021-10-27 17:30:00 00:00',
'end_date': '2021-10-30T21:00:00Z'}]
Another solution List Comprehension:
In [403]: l = [{"start_date": str(ele), "end_date": str(x[ix 1])} for ix, ele in enumerate(x) if ix 1 < len(x)]
In [404]: if l[-1]['end_date'] < end_date:
...: l.append({'start_date': l[-1]['end_date'], 'end_date':end_date})
...:
In [405]: l
Out[405]:
[{'start_date': '2021-10-01 17:30:00 00:00',
'end_date': '2021-10-14 17:30:00 00:00'},
{'start_date': '2021-10-14 17:30:00 00:00',
'end_date': '2021-10-27 17:30:00 00:00'},
{'start_date': '2021-10-27 17:30:00 00:00',
'end_date': '2021-10-30T21:00:00Z'}]
CodePudding user response:
try this:
import pandas as pd
start_date = '2021-10-01T17:30:00Z'
end_date = '2021-10-30T21:00:00Z'
s = pd.Series(pd.date_range(start_date, end_date ,freq='13D'), name='start_date')
e = s.shift(-1).rename('end_date')
df = pd.concat([s, e], axis=1).dropna(subset=['end_date']).astype('str')
df.to_dict('records')
>>>
[{'start_date': '2021-10-01 17:30:00 00:00',
'end_date': '2021-10-14 17:30:00 00:00'},
{'start_date': '2021-10-14 17:30:00 00:00',
'end_date': '2021-10-27 17:30:00 00:00'}]
CodePudding user response:
This should get you basically what you want (assuming you want the final dates as strings):
import pandas as pd
start_date = '2021-10-01 17:30:00'
end_date = '2021-10-30 21:00:00'
dates = list(pd.date_range(start_date, end_date, freq='13D').astype(str))
if dates[-1] != end_date:
dates.append(end_date)
result = [{'start_date': start, 'end_date': end} for start, end in zip(dates, dates[1:])]
