How Can we Loop Through a Range of Rolling Dates? - python

I did some Googling and figured out how to generate all Friday dates in a year.
# get all Fridays in a year
from datetime import date, timedelta
def allfridays(year):
d = date(year, 1, 1) # January 1st
d += timedelta(days = 8 - 2) # Friday
while d.year == year:
yield d
d += timedelta(days = 7)
for d in allfridays(2022):
print(d)
Result:
2022-01-07
2022-01-14
2022-01-21
etc.
2022-12-16
2022-12-23
2022-12-30
Now, I'm trying to figure out how to loop through a range of rolling dates, so like 2022-01-07 + 60 days, then 2022-01-14 + 60 days, then 2022-01-21 + 60 days.
step #1:
start = '2022-01-07'
end = '2022-03-08'
step #2:
start = '2022-01-14'
end = '2022-03-15'
Ideally, I want to pass in the start and end date loop, into another loop, which looks like this...
price_data = []
for ticker in tickers:
try:
prices = wb.DataReader(ticker, start = start.strftime('%m/%d/%Y'), end = end.strftime('%m/%d/%Y'), data_source='yahoo')[['Adj Close']]
price_data.append(prices.assign(ticker=ticker)[['ticker', 'Adj Close']])
except:
print(ticker)
df = pd.concat(price_data)

as you use pandas then you can try to do it this way:
import pandas as pd
year = 2022
dates = pd.date_range(start=f'{year}-01-01',end=f'{year}-12-31',freq='W-FRI')
df = pd.DataFrame({'my_dates':dates, 'sixty_ahead':dates + pd.Timedelta(days=60)})
print(df.head())
'''
my_dates sixty_ahead
0 2022-01-07 2022-03-08
1 2022-01-14 2022-03-15
2 2022-01-21 2022-03-22
3 2022-01-28 2022-03-29
4 2022-02-04 2022-04-05

First, we have to figure out how to get the first Friday of a given year. Next, we will calculate the start, end days.
import datetime
FRIDAY = 4 # Based on Monday=0
WEEK = datetime.timedelta(days=7)
def first_friday(year):
"""Return the first Friday of the year."""
the_date = datetime.date(year, 1, 1)
while the_date.weekday() != FRIDAY:
the_date = the_date + datetime.timedelta(days=1)
return the_date
def friday_ranges(year, days_count):
"""
Generate date ranges that starts on first Friday of `year` and
lasts for `days_count`.
"""
DURATION = datetime.timedelta(days=days_count)
start_date = first_friday(year)
end_date = start_date + DURATION
while end_date.year == year:
yield start_date, end_date
start_date += WEEK
end_date = start_date + DURATION
for start_date, end_date in friday_ranges(year=2022, days_count=60):
# Do what you want with start_date and end_date
print((start_date, end_date))
Sample output:
(datetime.date(2022, 1, 7), datetime.date(2022, 3, 8))
(datetime.date(2022, 1, 14), datetime.date(2022, 3, 15))
(datetime.date(2022, 1, 21), datetime.date(2022, 3, 22))
...
(datetime.date(2022, 10, 21), datetime.date(2022, 12, 20))
(datetime.date(2022, 10, 28), datetime.date(2022, 12, 27))
Notes
The algorithm for first Friday is simple: Start with Jan 1, then keep advancing the day until Friday
I made an assumption that the end date must fall into the specified year. If that is not the case, you can adjust the condition in the while loop

This could work maybe. You can add the condition, the end of the loop within the lambda function.
from datetime import date, timedelta
def allfridays(year):
d = date(year, 1, 1) # January 1st
d += timedelta(days = 8 - 2) # Friday
while d.year == year:
yield d
d += timedelta(days = 7)
list_dates = []
for d in allfridays(2022):
list_dates.append(d)
add_days = map(lambda x: x+timedelta(days = 60),list_dates)
print(list(add_days))

Oh my, I totally missed this before. The solution below works just fine.
import pandas as pd
# get all Fridays in a year
from datetime import date, timedelta
def allfridays(year):
d = date(year, 1, 1) # January 1st
d += timedelta(days = 8 - 2) # Friday
while d.year == year:
yield d
d += timedelta(days = 7)
lst=[]
for d in allfridays(2022):
lst.append(d)
df = pd.DataFrame(lst)
print(type(df))
df.columns = ['my_dates']
df['sixty_ahead'] = df['my_dates'] + timedelta(days=60)
df
Result:
my_dates sixty_ahead
0 2022-01-07 2022-03-08
1 2022-01-14 2022-03-15
2 2022-01-21 2022-03-22
etc.
49 2022-12-16 2023-02-14
50 2022-12-23 2023-02-21
51 2022-12-30 2023-02-28

Related

Print all thursdays between date range

I want to print all Thursdays between these date ranges
from datetime import date, timedelta
sdate = date(2015, 1, 7) # start date
edate = date(2015, 12, 31) # end date
What is the best pythonic way to do that?
using your sdate.weekday() # returns int between 0 (mon) and 6 (sun):
sdate = ...
while sdate < edate:
if sdate.weekday() != 3: # not thursday
sdate += timedelta(days=1)
continue
# It is thursday
print(sdate)
sdate += timedelta(days=7) # next week
Compute the number of days till thursday from the start date :
days_to_thursday = (3 - sdate.weekday()) % 7
Compute the number of thursdays inbetween both dates:
week_diff = ((edate - sdate).days - days_to_thursday ) // 7
Get all thursdays in the date range:
thursdays = [sdate + timedelta(days=days_to_thursday + 7 * more_weeks) \
for more_weeks in range(week_diff + 1) ]
Print them if you need:
for t in thursdays:
print(t)
Most people are iterating through every day which is a waste. Also might be helpful to delay calculating the thursdays until you actually need them. For this you could use a generator.
def get_days(start, day_index, end=None):
# set the start as the next valid day
start += timedelta(days=(day_index - start.weekday()) % 7)
week = timedelta(days=7)
while end and start < end or not end:
yield start
start += week
This delays getting the next day until you need it, and allows infinite days if you don't specify and end date.
thursday_generator = get_days(date(2015, 1, 7), 3, date(2015, 12, 31))
print(list(thursday_generator))
"""
[datetime.date(2015, 1, 8), datetime.date(2015, 1, 15), datetime.date(2015, 1, 22), ...]
"""
You can easily dump as strings:
print("\n".join(map(str, thursday_generator)))
"""
2015-01-08
2015-01-15
2015-01-22
...
"""
You can also use f-strings for custom string formatting:
print("\n".join(f"{day:%A %x}" for day in thursday_generator))
"""
Thursday 01/08/15
Thursday 01/15/15
Thursday 01/22/15
...
"""
If you don't specify an end date, it goes on forever.
In [28]: thursday_generator = get_days(date(2015, 1, 7), 3)
...: print(len(list(thursday_generator)))
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
<ipython-input-28-b161cdcccc75> in <module>
1 thursday_generator = get_days(date(2015, 1, 7), 3)
----> 2 print(len(list(thursday_generator)))
<ipython-input-16-0691db329606> in get_days(start, day_index, end)
5 while end and start < end or not end:
6 yield start
----> 7 start += week
8
OverflowError: date value out of range
You could try a list comprehension to get the Thursdays between the 2 dates.
This code actually outputs the dates as formatted strings but you can get actual dates by dropping the strftime.
from datetime import date, timedelta
sdate = date(2015, 1, 7) # start date
edate = date(2015, 12, 31) # end date
thursdays = [(sdate+timedelta(days=d)).strftime('%A %Y-%m-%d') for d in range(0, (edate-sdate).days+1)
if (sdate+timedelta(days=d)).weekday() ==3]
print('\n'.join(thursdays))
""" Example output
Thursday 2015-01-08
Thursday 2015-01-15
Thursday 2015-01-22
Thursday 2015-01-29
Thursday 2015-02-05
Thursday 2015-02-12
""""
import datetime
import calendar
def weekday_count(start, end, day):
start_date = datetime.datetime.strptime(start, '%d/%m/%Y')
end_date = datetime.datetime.strptime(end, '%d/%m/%Y')
day_count = []
for i in range((end_date - start_date).days):
if calendar.day_name[(start_date + datetime.timedelta(days=i+1)).weekday()] == day:
print(str(start_date + datetime.timedelta(days=i+1)).split()[0])
weekday_count("01/01/2017", "31/01/2017", "Thursday")
# prints result
# 2017-01-05
# 2017-01-12
# 2017-01-19
# 2017-01-26
Simple solution:
from datetime import date, timedelta
sdate = date(2015, 1, 7) # start date
edate = date(2015, 12, 31) # end date
delta = edate - sdate
for day in range(delta.days + 1):
day_obj = sdate + timedelta(days=day)
if day_obj.weekday() == 3: # Thursday
print(day_obj)
# 2015-01-08
# 2015-01-15
# ...
# 2015-12-24
# 2015-12-31
The most efficient solution:
from datetime import date, timedelta
sdate = date(2015, 1, 7) # start date
edate = date(2015, 12, 31) # end date
day_index = 3 # Thursday
delta = (day_index - sdate.weekday()) % 7
match = sdate + timedelta(days=delta)
while match <= edate: # Change this to `<` to ignore the last one
print(match) # Can be easily converted to a generator with `yield`
match += timedelta(days=7)
# 2015-01-08
# 2015-01-15
# ...
# 2015-12-24
# 2015-12-31
Docs:
.weekday(): https://docs.python.org/3/library/datetime.html#datetime.date.weekday
.timedelta(): https://docs.python.org/3/library/datetime.html#timedelta-objects
% operator on negative numbers: The modulo operation on negative numbers in Python

Is there any function calculate duration in minutes between two datetimes values?

This is my dataframe.
Start_hour End_date
23:58:00 00:26:00
23:56:00 00:01:00
23:18:00 23:36:00
How can I get in a new column the difference (in minutes) between these two columns?
>>> from datetime import datetime
>>>
>>> before = datetime.now()
>>> print('wait for more than 1 minute')
wait for more than 1 minute
>>> after = datetime.now()
>>> td = after - before
>>>
>>> td
datetime.timedelta(seconds=98, microseconds=389121)
>>> td.total_seconds()
98.389121
>>> td.total_seconds() / 60
1.6398186833333335
Then you can round it or use it as-is.
You can do something like this:
import pandas as pd
df = pd.DataFrame({
'Start_hour': ['23:58:00', '23:56:00', '23:18:00'],
'End_date': ['00:26:00', '00:01:00', '23:36:00']}
)
df['Start_hour'] = pd.to_datetime(df['Start_hour'])
df['End_date'] = pd.to_datetime(df['End_date'])
df['diff'] = df.apply(
lambda row: (row['End_date']-row['Start_hour']).seconds / 60,
axis=1
)
print(df)
Start_hour End_date diff
0 2021-03-29 23:58:00 2021-03-29 00:26:00 28.0
1 2021-03-29 23:56:00 2021-03-29 00:01:00 5.0
2 2021-03-29 23:18:00 2021-03-29 23:36:00 18.0
You can also rearrange your dates as string again if you like:
df['Start_hour'] = df['Start_hour'].apply(lambda x: x.strftime('%H:%M:%S'))
df['End_date'] = df['End_date'].apply(lambda x: x.strftime('%H:%M:%S'))
print(df)
Output:
Start_hour End_date diff
0 23:58:00 00:26:00 28.0
1 23:56:00 00:01:00 5.0
2 23:18:00 23:36:00 18.0
Short answer:
df['interval'] = df['End_date'] - df['Start_hour']
df['interval'][df['End_date'] < df['Start_hour']] += timedelta(hours=24)
Why so:
You probably trying to solve the problem that your Start_hout and End_date values sometimes belong to a different days, and that's why you can't just substutute one from the other.
It your time window never exceeds 24 hours interval, you could use some modular arithmetic to deal with 23:59:59 - 00:00:00 border:
if End_date < Start_hour, this always means End_date belongs to a next day
this implies, if End_date - Start_hour < 0 then we should add 24 hours to End_date to find the actual difference
The final formula is:
if rec['Start_hour'] < rec['End_date']:
offset = 0
else:
offset = timedelta(hours=24)
rec['delta'] = offset + rec['End_date'] - rec['Start_hour']
To do the same with pandas.DataFrame we need to change code accordingly. And
that's how we get the snippet from the beginning of the answer.
import pandas as pd
df = pd.DataFrame([
{'Start_hour': datetime(1, 1, 1, 23, 58, 0), 'End_date': datetime(1, 1, 1, 0, 26, 0)},
{'Start_hour': datetime(1, 1, 1, 23, 58, 0), 'End_date': datetime(1, 1, 1, 23, 59, 0)},
])
# ...
df['interval'] = df['End_date'] - df['Start_hour']
df['interval'][df['End_date'] < df['Start_hour']] += timedelta(hours=24)
> df
Start_hour End_date interval
0 0001-01-01 23:58:00 0001-01-01 00:26:00 0 days 00:28:00
1 0001-01-01 23:58:00 0001-01-01 23:59:00 0 days 00:01:00

Python - Iterate thru month dates and print a custom output

I'm currently unsure on the logic to be used for the below problem and new to programming as well.(Currently learning python)
Trying to iterate thru every date for a given month - say 05/01 -- 05/31 and print it out in the below format.
Monday thru Friday dates are to be printed separately.
Saturday & Sunday dates are to be printed separately.
If the month starts on say Friday - 05/01/2020, ouput should be like
as, its the last weekday of that week.
For the month of April 2020, output would be like below, as April month's 1st week started on Wednesday.
I managed to comeup with the below try, but not sure how to proceed further.
import sys
from datetime import date, datetime, timedelta
year = int(sys.argv[1])
month = int(sys.argv[2])
st_dt = int(sys.argv[3])
en_dt = int(sys.argv[4])
first_date = datetime(year, month, st_dt).date()
get_first_day = datetime(year, month, st_dt).isoweekday()
def daterange(startDate, endDate, delta=timedelta(days=1)):
currentDate = startDate
while currentDate <= endDate:
yield currentDate
currentDate += delta
for date in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
print(date)
date.py 2020 5 1 31 # script
Came up with a standalone 'if loop' and as i said before, not sure how to construct the bigger picture :(
if get_first_day == 1:
#print("Monday")
sec_d = first_date + timedelta(days=4)
elif get_first_day == 2:
sec_d = first_date + timedelta(days=3)
elif get_first_day == 3:
sec_d = first_date + timedelta(days=2)
elif get_first_day == 4:
sec_d = first_date + timedelta(days=2)
elif get_first_day == 5:
sec_d = first_date
#print("Friday")
else:
pass
print(f"Second date:{sec_d} ") -- which gave -- > Second date:2020-05-01
You could keep the dates in a dictionary, dictionary key is tuple of calendar week and type of day (weekend, day of the week).
Each day is saved by in the allDays dictionary, grouped by the combination of weeknum and type of day as key:
('18', 'weekend'): [datetime.date(2020, 5, 2), datetime.date(2020, 5, 3)],
('18', 'working'): [datetime.date(2020, 5, 1)],
('19', 'weekend'): [datetime.date(2020, 5, 9), datetime.date(2020, 5, 10)],
('19', 'working'): [datetime.date(2020, 5, 4), ...
So you just need to take out the fist and last item of each dict item:
import sys
from datetime import date, datetime, timedelta
year, month, st_dt, en_dt = 2020, 5, 1, 31
first_date = datetime(year, month, st_dt).date()
get_first_day = datetime(year, month, st_dt).isoweekday()
def daterange(startDate, endDate, delta=timedelta(days=1)):
currentDate = startDate
while currentDate <= endDate:
yield currentDate
currentDate += delta
allDays = {}
_lastDayType = None
for dte in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
if 0 <= dte.weekday() < 5:
_dayType = 'working'
else:
_dayType = 'weekend'
_weeknum = dte.strftime("%V") # number of calendar week
_key = (_weeknum, _dayType)
if _key not in allDays: # create an empty list if unique key doesnt exist
allDays[_key] = []
allDays[_key].append(dte) # add the dates ...
for k,v in allDays.items():
if len(v) == 1:
first, last = v[0], v[0]
else:
first, last = v[0], v[-1]
print("%s >> %s" % (first, last))
Output:
2020-05-01 >> 2020-05-01
2020-05-02 >> 2020-05-03
2020-05-04 >> 2020-05-08
2020-05-09 >> 2020-05-10
2020-05-11 >> 2020-05-15
2020-05-16 >> 2020-05-17
2020-05-18 >> 2020-05-22
2020-05-23 >> 2020-05-24
2020-05-25 >> 2020-05-29
2020-05-30 >> 2020-05-31

Check if it is the end of the month in python

I am writing code to take data from the last year. I want to round up the earlier date like so: If it is July 14 2015, I want data from August 1st 2014-July 14,2015
df = pd.read_csv('MyData.csv')
df['recvd_dttm'] = pd.to_datetime(df['recvd_dttm'])
range_max = datetime.datetime.now()
range_min = range_max - pd.tseries.offsets.DateOffset(years=1)+ pd.tseries.offsets.MonthEnd(1) + pd.tseries.offsets.DateOffset(days=1)
if datetime.datetime.now() == is_month_end:
# take slice with final week of data
df = df[(df['recvd_dttm'] >= range_min) &
(df['recvd_dttm'] <= range_max)]
My problem is that when it is July 31, 2015, my code goes to the end of the next month, essentially cutting out an entire month.
I am trying to make a for loop to fix this problem.
If it is the end of the month:
range_min = range_max - pd.tseries.offsets.DateOffset(years=1)
else:
range_min = range_max - pd.tseries.offsets.DateOffset(years=1)+ pd.tseries.offsets.MonthEnd(1) + pd.tseries.offsets.DateOffset(days=1)
How do I tell python to check for the end of the month? MonthEnd is only an offset function.
We can avoid importing the calendar module with a short function that only leverages datetime.
If tomorrow's month is not the same as today's month, then that means today is the last day of the current month. We can check this programmatically with a short function such as
import datetime
def end_of_month(dt):
todays_month = dt.month
tomorrows_month = (dt + datetime.timedelta(days=1)).month
return tomorrows_month != todays_month
Now, for your specific use case:
now = datetime.datetime.now()
if end_of_month(now):
range_min = range_max - pd.tseries.offsets.DateOffset(years=1)
else:
range_min = range_max - pd.tseries.offsets.DateOffset(years=1) +pd.tseries.offsets.MonthEnd(1) + pd.tseries.offsets.DateOffset(days=1)
I simply would use the monthrange method of calendar module to find last day number of the month:
def check_if_last_day_of_week(date):
import datetime
import calendar
# calendar.monthrange return a tuple (weekday of first day of the
# month, number
# of days in month)
last_day_of_month = calendar.monthrange(date.year, date.month)[1]
# here i check if date is last day of month
if date == datetime.date(date.year, date.month, last_day_of_month):
return True
return False
>>> date = datetime.date(2018, 12, 31)
>>> check_if_last_day_of_week(date)
True
If the next day is a different month, it means it is the last day of a month.
def check_if_last_day_of_month(to_date):
delta = datetime.timedelta(days=1)
next_day = to_date + delta
if to_date.month != next_day.month:
return True
return False
I was using Pandas and I did not want to include another library, so I used this to check whether is the last day of the month and last day of the year:
import pandas as pd
my_date = '31-12-2021'
current_data = pd.to_datetime(my_date, format='%d-%m-%Y')
current_month = current_data.month
current_year = current_data.year
following_day = current_data + pd.DateOffset(1)
tomorrows_month = following_day.month
tomorrows_year = following_day.year
is_last_day_of_month = True if tomorrows_month != current_month else False
is_last_day_of_year = True if tomorrows_year != current_year else False
Here's a pure python approach that also takes into account leap years for february:
# total days in every month during non leap years
M_DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def days_in_month(year, month):
"""Returns total number of days in a month accounting for leap years."""
return M_DAYS[month] + (month == 2 and isleap(year))
def is_monthend(ref_date):
"""Checks whether a date is also a monthend"""
return ref_date.day == days_in_month(ref_date.year, ref_date.month)
Alright, here's what I did. Found the calendar module that BryanOakley suggested and made this loop. It checks the current day and checks if it is the same as the last day of the month, and chooses the range_min accordingly.
if datetime.datetime.now().day == calendar.monthrange(date.year, date.month)[1]:
range_min = range_max - pd.tseries.offsets.DateOffset(years=1)+ pd.tseries.offsets.DateOffset(days=1)
else:
range_min = range_max - pd.tseries.offsets.DateOffset(years=1)+ pd.tseries.offsets.MonthEnd(1) + pd.tseries.offsets.DateOffset(days=1)
import datetime
def find_curr_month_end_date(curr_date):
if(curr_date.month != 12):
next_month_first_date= curr_date.replace(day=1).replace(month=curr_date.month+1)
else:
next_month_first_date= curr_date.replace(day=1).replace(month=1).replace(year=curr_date.year+1)
curr_month_end_date = next_month_first_date - datetime.timedelta(days=1)
return curr_month_end_date
curr_date = datetime.datetime.today()
# or curr_date = datetime.datetime.strptime("2020-12-16","%Y-%m-%d")
curr_month_end_date =
find_curr_month_end_date(curr_date)
Here is a short function to accomplish this. It requires the dateutil module so that you can do relative date math.
import datetime
from dateutil.relativedelta import relativedelta
def lastyear_period_start(current_date):
last_year = current_date - relativedelta(months=11)
return datetime.date(last_year.year, last_year.month, 1)
It can be utilized like so:
dates = [
datetime.datetime(2010, 2, 27),
datetime.datetime(2011, 2, 27),
datetime.datetime(2012, 2, 27),
datetime.datetime(2013, 2, 27),
datetime.datetime(2014, 2, 27),
datetime.datetime(2010, 7, 27),
datetime.datetime(2011, 7, 27),
datetime.datetime(2012, 7, 27),
datetime.datetime(2013, 7, 27),
datetime.datetime(2014, 7, 27),
datetime.datetime(2015, 7, 14),
datetime.datetime(2015, 7, 31),
datetime.datetime(2011, 2, 28),
datetime.datetime(2012, 2, 29),
datetime.datetime(2013, 2, 28),
]
for d in dates:
print d, lastyear_period_start(d)
This prints that following
2010-02-27 00:00:00 2009-03-01
2011-02-27 00:00:00 2010-03-01
2012-02-27 00:00:00 2011-03-01
2013-02-27 00:00:00 2012-03-01
2014-02-27 00:00:00 2013-03-01
2010-07-27 00:00:00 2009-08-01
2011-07-27 00:00:00 2010-08-01
2012-07-27 00:00:00 2011-08-01
2013-07-27 00:00:00 2012-08-01
2014-07-27 00:00:00 2013-08-01
2015-07-14 00:00:00 2014-08-01
2015-07-31 00:00:00 2014-08-01
2011-02-28 00:00:00 2010-03-01
2012-02-29 00:00:00 2011-03-01
2013-02-28 00:00:00 2012-03-01
In the function we're doing two simple steps
last_year = current_date - relativedelta(months=11)
First we find out what the date was 11 months ago, based on the date passed to the function
return datetime.date(last_year.year, last_year.month, 1)
Then we return the first day of that month.
In the output above you can see this accounts for leap years as well.

Find the date for the first Monday after a given date

Given a particular date, say 2011-07-02, how can I find the date of the next Monday (or any weekday day for that matter) after that date?
import datetime
def next_weekday(d, weekday):
days_ahead = weekday - d.weekday()
if days_ahead <= 0: # Target day already happened this week
days_ahead += 7
return d + datetime.timedelta(days_ahead)
d = datetime.date(2011, 7, 2)
next_monday = next_weekday(d, 0) # 0 = Monday, 1=Tuesday, 2=Wednesday...
print(next_monday)
Here's a succinct and generic alternative to the slightly weighty answers above.
def onDay(date, day):
"""
Returns the date of the next given weekday after
the given date. For example, the date of next Monday.
NB: if it IS the day we're looking for, this returns 0.
consider then doing onDay(foo, day + 1).
"""
days = (day - date.weekday() + 7) % 7
return date + datetime.timedelta(days=days)
Try
>>> dt = datetime(2011, 7, 2)
>>> dt + timedelta(days=(7 - dt.weekday()))
datetime.datetime(2011, 7, 4, 0, 0)
using, that the next monday is 7 days after the a monday, 6 days after a tuesday, and so on, and also using, that Python's datetime type reports monday as 0, ..., sunday as 6.
This is example of calculations within ring mod 7.
import datetime
def next_day(given_date, weekday):
day_shift = (weekday - given_date.weekday()) % 7
return given_date + datetime.timedelta(days=day_shift)
now = datetime.date(2018, 4, 15) # sunday
names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday',
'saturday', 'sunday']
for weekday in range(7):
print(names[weekday], next_day(now, weekday))
will print:
monday 2018-04-16
tuesday 2018-04-17
wednesday 2018-04-18
thursday 2018-04-19
friday 2018-04-20
saturday 2018-04-21
sunday 2018-04-15
As you see it's correctly give you next monday, tuesday, wednesday, thursday friday and saturday. And it also understood that 2018-04-15 is a sunday and returned current sunday instead of next one.
I'm sure you'll find this answer extremely helpful after 7 years ;-)
Another alternative uses rrule
from dateutil.rrule import rrule, WEEKLY, MO
from datetime import date
next_monday = rrule(freq=WEEKLY, dtstart=date.today(), byweekday=MO, count=1)[0]
rrule docs: https://dateutil.readthedocs.io/en/stable/rrule.html
Another simple elegant solution is to use pandas offsets.
I find it very helpful and robust when playing with dates.
If you want the first Sunday just modify the frequency to freq='W-SUN'.
If you want a couple of next Sundays, change the offsets.Day(days).
Using pandas offsets allow you to ignore holidays, work only with Business Days and more.
You can also apply this method easily on a whole DataFrame using the apply method.
import pandas as pd
import datetime
# 1. Getting the closest monday from a given date
date = datetime.date(2011, 7, 2)
closest_monday = pd.date_range(start=date, end=date + pd.offsets.Day(6), freq="W-MON")[
0
]
# 2. Adding a 'ClosestMonday' column with the closest monday for each row in
# a pandas df using apply. Requires you to have a 'Date' column in your df
def get_closest_monday(row):
return pd.date_range(
start=row.Date, end=row.Date + pd.offsets.Day(6), freq="W-MON"
)[0]
df = pd.DataFrame([datetime.date(2011, 7, 2)], columns=["Date"])
df["ClosestMonday"] = df.apply(lambda row: get_closest_monday(row), axis=1)
print(df)
You can start adding one day to date object and stop when it's monday.
>>> d = datetime.date(2011, 7, 2)
>>> while d.weekday() != 0: #0 for monday
... d += datetime.timedelta(days=1)
...
>>> d
datetime.date(2011, 7, 4)
import datetime
d = datetime.date(2011, 7, 2)
while d.weekday() != 0:
d += datetime.timedelta(1)
dateutil has a special feature for this kind of operation and it's the most elegant way I have ever seen yet.
from datetime import datetime
from dateutil.relativedelta import relativedelta, MO
first_monday_date = (datetime(2011,7,2) + relativedelta(weekday=MO(0))).date()
if you want datetime just
first_monday_date = datetime(2011,7,2) + relativedelta(weekday=MO(0))
weekday = 0 ## Monday
dt = datetime.datetime.now().replace(hour=0, minute=0, second=0) ## or any specific date
days_remaining = (weekday - dt.weekday() - 1) % 7 + 1
next_dt = dt + datetime.timedelta(days_remaining)
Generally to find any date from day of week from today:
def getDateFromDayOfWeek(dayOfWeek):
week_days = ["monday", "tuesday", "wednesday",
"thursday", "friday", "saturday", "sunday"]
today = datetime.datetime.today().weekday()
requiredDay = week_days.index(dayOfWeek)
if today>requiredDay:
noOfDays=7-(today-requiredDay)
print("noDays",noOfDays)
else:
noOfDays = requiredDay-today
print("noDays",noOfDays)
requiredDate = datetime.datetime.today()+datetime.timedelta(days=noOfDays)
return requiredDate
print(getDateFromDayOfWeek('sunday').strftime("%d/%m/%y"))
Gives output in format of Day/Month/Year
This will give the first next Monday after given date:
import datetime
def get_next_monday(year, month, day):
date0 = datetime.date(year, month, day)
next_monday = date0 + datetime.timedelta(7 - date0.weekday() or 7)
return next_monday
print get_next_monday(2011, 7, 2)
print get_next_monday(2015, 8, 31)
print get_next_monday(2015, 9, 1)
2011-07-04
2015-09-07
2015-09-07
via list comprehension?
from datetime import *
[datetime.today()+timedelta(days=x) for x in range(0,7) if (datetime.today()+timedelta(days=x)).weekday() % 7 == 0]
(0 at the end is for next monday, returns current date when run on monday)

Categories