Search code examples
pythonpandasdataframedayofweekweek-number

how do we consider sunday as the start of week when applying .dt.week to calculate week number


.dt.week function by default considers monday as start of the week but is there a way to consider sunday as start of the week Example:

analysis['week no']=(analysis['Date based Discharge'].dt.week)

Output:

Date based Discharge   week no
06-10-2019              wk 40
07-10-2019              wk 41
.........               .. ..

Desired output:

Date based Discharge   week no
06-10-2019              wk 41
07-10-2019              wk 41

Solution

  • Can you simply move each date forward one day before getting the week index?

    For example:

    (analysis['Date based Discharge'] + pd.DateOffset(1)).dt.week
    

    I think this should give the desired result.