I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python

Introduction

Motivation

I created it because it was necessary to automatically acquire the number of holidays in the month when creating the data necessary for machine learning.

I saw many articles that judge weekdays (business days), but since there were few articles related to holiday judgment (acquisition of days), I decided to post it with a memorial meaning.

Library used

-JPBizDay: Library to get business days in Japan

--datetime: python standard library, provides classes for manipulating dates and times

--calendar: python standard library, handles date and time

Execution environment

GoogleColaboratory

Preparation

First, install jpbizday.
When installing on colab, you need to add! Before pip.

!pip install jpbizday

Then import the required modules

import jpbizday
import datetime
import calendar

Implementation

procedure

  1. Get today's year / month
  2. Obtain the number of days of this month based on the acquired year / month
  3. Obtain the number of weekdays of this month based on the acquired year / month
  4. Get the number of holidays by subtracting the number of weekdays from the number of days this month
#Acquisition of today's year / month
dt_now = datetime.datetime.now()
year = dt_now.year
month = dt_now.month

#Get the number of days of the month
days = calendar.monthrange(year, month)[1]

#Get the number of weekdays
heijitu = jpbizday.month_bizdays(year, month)
heijitu = len(a)

#Get the number of days off
holiday = days - heijitu
print(holiday)

I think that it was possible to implement it fairly simply by using jpbizday. There are other ways! Isn't this implementation method better? If you have any advice, please feel free to contact us!


The site that I used as a reference

--How to use the calendar module
https://note.nkmk.me/python-days-weeks-of-the-month/ --How to use jpbizday
https://pypi.org/project/jpbizday/

Recommended Posts

I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
[Python] How to get the first and last days of the month
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
I tried to find the entropy of the image with python
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to compare the processing speed with dplyr of R and pandas of Python
I want to output the beginning of the next month with Python
I tried to improve the efficiency of daily work with Python
PhytoMine-I tried to get the genetic information of plants with Python
I tried to get CloudWatch data with Python
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I tried to verify and analyze the acceleration of Python by Cython
Get the number of articles accessed and likes with Qiita API + Python
I tried to streamline the standard role of new employees with Python
How to get the number of digits in Python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to solve the problem with Python Vol.1
Note: How to get the last day of the month with python (added the first day of the month)
I want to get the file name, line number, and function name in Python 3.4
[Python] I tried to get Json of squid ring 2
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to summarize the string operations of Python
I tried to solve AOJ's number theory with Python
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried to put out the frequent word ranking of LINE talk with Python
The 15th offline real-time I tried to solve the problem of how to write with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to simulate how the infection spreads with Python
I tried to get the location information of Odakyu Bus
I tried to find the average of the sequence with TensorFlow
[Python] I tried to visualize the follow relationship of Twitter
I want to know the features of Python and pip
I tried to enumerate the differences between java and python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python
Get the source of the page to load infinitely with python.
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
How to write offline real time I tried to solve the problem of F02 with Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I also tried to imitate the function monad and State monad with a generator in Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I compared the speed of Hash with Topaz, Ruby and Python
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to get the index of the list using the enumerate function
I tried to automate the watering of the planter with Raspberry Pi
[Introduction to Python] I compared the naming conventions of C # and Python.
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to make a periodical process with Selenium and Python
I tried to get started with Bitcoin Systre on the weekend
I tried to expand the size of the logical volume with LVM
I tried to easily detect facial landmarks with python and dlib
I tried to automatically collect images of Kanna Hashimoto with Python! !!
Introduction to AI creation with Python! Part 1 I tried to classify and predict what the numbers are from the handwritten number images.