Fazia Chenna
Fazia Chenna

Reputation: 135

how to calculate number of working days with python

I have a dataframe (df):

    year  month ETP
0   2021    1   49.21
1   2021    2   34.20
2   2021    3   31.27
3   2021    4   29.18
4   2021    5   33.25
5   2021    6   24.70

I would like to add a column that gives me the number of working days for each row excluding holidays and weekends (for a specific country, exp: France or US)

so the output will be :

    year  month ETP     work_day
0   2021    1   49.21      20
1   2021    2   34.20      20
2   2021    3   31.27      21
3   2021    4   29.18      19
4   2021    5   33.25      20
5   2021    6   24.70      19

code :

import numpy as np
import pandas as pd
days = np.busday_count( '2021-01', '2021-06' ) 
df.insert(3, "work_day", [days]) 

and I got this error :

ValueError: Length of values does not match length of index

Any suggestions?

Thank you for your help

Upvotes: 1

Views: 1508

Answers (1)

Tony
Tony

Reputation: 764

assuming you are the one that will input the workdays, I suppose you can do it like this:

data = {'year': [2020, 2020, 2021, 2023, 2022], 
        'month': [1, 2, 3, 4, 6]} 
 
df = pd.DataFrame(data)
 
df.insert(2, "work_day", [20,20,23,21,22]) 

Where the 2 is the position of the new column, not just to be at the end, work_day is the name and the list has the values for every row.

EDIT: With NumPy

import numpy as np
import pandas as pd

days = np.busday_count( '2021-02', '2021-03' )
data = {'year': [2021], 
        'month': ['february']} 
 
df = pd.DataFrame(data)
 
df.insert(2, "work_day", [days]) 

with the busday_count you specify the starting and ending dates you want to see the workdays in. the result :

   year     month  work_day
0  2021  february  20

Upvotes: 2

Related Questions