How to set a custom day of week mapping

Question:

I am trying to create my own custom day of the week mapping using python. I have used a few different methods such as dayofweek and isoweekday. Yet all of these dont provide me with what I need.


0    2026-01-01 00:00:00
1    2026-01-01 01:00:00
2    2026-01-01 02:00:00
3    2026-01-01 03:00:00
4    2026-01-01 04:00:00
5    2026-01-01 05:00:00
6    2026-01-01 06:00:00
7    2026-01-01 07:00:00
8    2026-01-01 08:00:00
9    2026-01-01 09:00:00
10   2026-01-01 10:00:00
11   2026-01-01 11:00:00
12   2026-01-01 12:00:00
13   2026-01-01 13:00:00
14   2026-01-01 14:00:00
15   2026-01-01 15:00:00
16   2026-01-01 16:00:00
17   2026-01-01 17:00:00
18   2026-01-01 18:00:00
19   2026-01-01 19:00:00
Name: Date, dtype: datetime64[ns]

It contiunes to

0    2026-01-01 00:00:00
1    2026-01-01 01:00:00
2    2026-01-01 02:00:00
3    2026-01-01 03:00:00
4    2026-01-01 04:00:00
       
95   2026-01-04 23:00:00
96   2026-01-05 00:00:00
97   2026-01-05 01:00:00
98   2026-01-05 02:00:00
99   2026-01-05 03:00:00

example of my code
power_data['Day of Week'] = power_data['Date'].dt.dayofweek+1
power_data['Day of Weekv2'] = power_data['Date'].dt.isoweekday

Above is a example portion of my dataframe, the data formatting I would like to follow is Sunday be 1, monday be 2…etc and Saturday be equal to 7. Please let me know if I can do this how its currently presented

Asked By: ARE

||

Answers:

As per pandas documentation for weekday, it mentions that:

The day of the week with Monday=0, Sunday=6.

Which means, you just need to add 2 to the weekday value as you want to shift Monday from 0 to 2, then take modulo by 8

# df is your dataframe, and date is the column name consisting pandas Timestamp
>>> (df['date'].dt.weekday+2)%8

#output:
0     5
1     5
2     5
3     5
4     5
5     5
6     5
7     5
8     5
9     5
10    5
11    5
12    5
13    5
14    5
15    5
16    5
17    5
18    5
Name: date, dtype: int64
Answered By: ThePyGuy
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.