Get date range of week from a given date range - python

I've been trying to break a given date range into date range each week, but couldn't find any effective way to do it.
Let's say I have input: 05/05/2020 (Tues) - 04/06/2020, and the out put would be like
('05/05/2020', '10/05/2020'),
('11/05/2020', '17/05/2020'),
....
Any help would be appreciated, thank you so much^

you should start a loop from the first date and run it till you find the first Sunday. When you hit the first Sunday, You get your first week. After that just keep adding 7 days to get the next week. While adding 7 days check if next Sunday date is falling within the provided range. if it is going outside the range, set the last date of the provided range as the ending date of last week instead of Sunday.

Related

How can I include the end date in np.busday_count?

By default, the function excludes the last date while calculating business days. Adding 1 is not a good idea. If the last day is a Sunday and adding 1 will give incorrect business days. Is there a parameter that handles this or a way to get business days with the last date included?

Replacing dates that fall on weekends to next business day in dataframe

I have a dataframe with bunch of dates in them, I would like to check each entry if it is a weekday or weekend, if it is a weekend i would like to increase the date to the next weekday. What is the most pythonic way of doing this ? I was thinking about using a list comprehension and s.th like
days = pd.date_range(start='1/1/2020', end='1/08/2020')
dates = pd.DataFrame(days,columns=['dates'])
dates['dates'] = [day+pd.DateOffset(days=1) if day.weekday() >4 else day for day in dates['dates']]
How could I adjust the code to cover day+pd.DateOffset(days=1) (for Sunday) and day+pd.DateOffset(days=2) (for Saturday) and get an updated column with the shifted dates? Running the code twice with +1 should work but is certainly not pretty

Calculating week number of year (dealing with first week of year)

I am trying to convert the date into week number of year.
In my case, I cannot include the day of one year into another year.
The isocalendar() works fine to find week number, however, it assumes some rules: if the first week in January has less than 4 days, it is counted as the last week of the year before.
So, this function returns:
date(2016, 1, 1).isocalendar()[1]
53
Is there some way, using this function, to change this, to return week 0 instead of week 53 (from previous year) ?
how about this?
import datetime
datetime.date(2016, 1, 1).strftime("%U")

Python: Date conversion to year-weeknumber, issue at switch of year

I am trying to convert a dataframe column with a date and timestamp to a year-weeknumber format, i.e., 01-05-2017 03:44 = 2017-1. This is pretty easy, however, I am stuck at dates that are in a new year, yet their weeknumber is still the last week of the previous year. The same thing that happens here.
I did the following:
df['WEEK_NUMBER'] = df.date.dt.year.astype(str).str.cat(df.date.dt.week.astype(str), sep='-')
Where df['date'] is a very large column with date and times, ranging over multiple years.
A date which gives a problem is for example:
Timestamp('2017-01-01 02:11:27')
The output for my code will be 2017-52, while it should be 2016-52. Since the data covers multiple years, and weeknumbers and their corresponding dates change every year, I cannot simply subtract a few days.
Does anybody have an idea of how to fix this? Thanks!
Replace df.date.dt.year by this:
(df.date.dt.year- ((df.date.dt.week>50) & (df.date.dt.month==1)))
Basically, it means that you will substract 1 to the year value if the week number is greater than 50 and the month is January.

Get the dates by entering the number of days

I was experimenting with something on Python and came across an interesting problem. I would like to enter the number of days and get the last and today's date in the range I specified. BUT, I only want the dates for the business days (Exclusing Holidays and Weekends). For example, I would like to do:
previous_days(10)
The output would give me the date that was 10 days ago and also today's date:
'2017-02-17' #10-days ago (because the 20th was President's day)
'2017-03-03' #Today's date
This is what I have been doing so far:
todaysDate = time.strftime("%Y-%m-%d") #outputs 2017-03-03
tenDaysAgo = datetime.strptime(todaysDate, "%Y-%m-%d").date() + timedelta(days=-12)
I had to do -12 to take into account the weekends. But the dates were incorrect this time because of President's day. Is there a library that exists in Python where I can enter the number of days I want and I can get the dates excluding weekends and holidays? If not, is there a more clever way for me to go about solving my issue?
Not enough reputation for a comment...
If DYZ's comment didn't help, take a look at dateutil.
More Dateutil examples
Other possibilities on SO here and here

Categories

Resources