Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue the week number when the year changes using pandas

Example: By using

df['Week_Number'] = df['Date'].dt.strftime('%U') 
for 29/12/2019 the week is 52. and this week is from 29/12/2019 to 04/01/2020.

but for 01/01/2020 the week is getting as 00.

I require the week for 01/01/2020 also as 52. and for 05/01/2020 to 11/01/2020 as 53. This need to be continued.

like image 554
Suhas_mudam Avatar asked Nov 15 '22 19:11

Suhas_mudam


1 Answers

I used a logic to solve the question.

First of all, let's write a function to create an instance of Dataframe involving dates from 2019-12-01 to 2020-01-31 by a function

def create_date_table(start='2019-12-01', end='2020-01-31'):
    df = pd.DataFrame({"Date": pd.date_range(start, end)})
    df["Week_start_from_Monday"] = df.Date.dt.isocalendar().week
    df['Week_start_from_Sunday'] = df['Date'].dt.strftime('%U')
    return df

Run the function and observe the Dataframe

date_df=create_date_table()
date_df.head(n=40)

enter image description here

There are two fields in the Dataframe about weeks, Week_start_from_Monday and Week_start_from_Sunday, the difference come from they count Monday or Sunday as the first day of a week.

In this case, Week_start_from_Sunday is the one we need to focus on.

Now we write a function to add a column containing weeks continuing from last year, not reset to 00 when we enter a new year.

def add_continued_week_field(date: Timestamp, df_start_date: str = '2019-12-01') -> int:
    start_date = datetime.strptime(df_start_date, '%Y-%m-%d')
    year_of_start_date = start_date.year
    year_of_date = date.year
    week_of_date = date.strftime("%U")
    year_diff = year_of_date - year_of_start_date
    if year_diff == 0:
        continued_week = int(week_of_date)
    else:
        continued_week = year_diff * 52 + int(week_of_date)
    return continued_week

Let's apply the function add_continued_week_field to the dates' Dataframe.

date_df['Week_continue'] = date_df['Date'].apply(add_continued_week_field)

We can see the new added field in the dates' Dataframe enter image description here

like image 166
BoHuang Avatar answered Dec 10 '22 16:12

BoHuang