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

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?

Related

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

Get date range of week from a given date range

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.

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

Algorithm to get the current week number (not ISO)

I'm looking for an easy way to get the current week number of the year in Python. I'm well aware of the datetime.datetime.isocalendar() function in the standard library, but this function stipulates that week 1 is the first Monday of the new year. My dilemma is that I'm using Sunday as a starting point for each week, and if Sunday is for example December 27 and January 1st appears at some point during that week, I need to represent that week as week 1 (and year 2015).
I thought of doing something like (pseudocode):
if (Jan 1) - (current_sunday) < 7 days:
week_num = 1
And then storing that week number somewhere to iterate over next week. However, I feel that this is a very hackish method and would prefer something cleaner.
Generally to get the current week number (starts from Sunday):
>>> import datetime
>>> import calendar
>>> today = datetime.date.today())
>>> (int(today.strftime('%U')) + (datetime.date(today.year+1, 1, 1).weekday() != calendar.SUNDAY)) % 53
12
From the documentation of strftime('%U'):
"Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0."
Hence the modified code for your specific requirements. There isn't really a non-hacky way to do what you want.

Categories

Resources