How to Get Week Number from Date in Pandas


Often you may want to get the week number from a date in a pandas DataFrame.

You can use the following methods to do so:

Method 1: Get Week Number from Date

df['week_num'] = df['date'].dt.strftime('%U')

This particular example creates a new column named week_num that contains the week number (as a two-digital decimal number) of the corresponding date in the date column of the DataFrame.

Method 2: Get Week Number and Year from Date

df['week_num'] = df['date'].dt.strftime('%Y-%U')

This particular example creates a new column named week_num that contains the year and the week number of the corresponding date in the date column of the DataFrame.

This method is particularly useful if you have dates in the date column that span multiple years.

The following examples show how to use each method in practice.

Example 1: Get Week Number from Date in Pandas

Suppose we create the following pandas DataFrame that contains information about total sales made by some employee on various dates at some company:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'date': pd.date_range('1/1/2024', periods=10, freq='2W'),
                   'sales': [2, 5, 5, 4, 7, 8, 9, 12, 10, 14]})

#view DataFrame
print(df)

        date  sales
0 2024-01-07      2
1 2024-01-21      5
2 2024-02-04      5
3 2024-02-18      4
4 2024-03-03      7
5 2024-03-17      8
6 2024-03-31      9
7 2024-04-14     12
8 2024-04-28     10
9 2024-05-12     14

Suppose that we would like to extract the week number for each date in the date column of the DataFrame.

We can use the following syntax to do so:

#create new column that contains week number of date
df['week_num'] = df['date'].dt.strftime('%U')

#view updated DataFrame
print(df)

        date  sales week_num
0 2024-01-07      2       01
1 2024-01-21      5       03
2 2024-02-04      5       05
3 2024-02-18      4       07
4 2024-03-03      7       09
5 2024-03-17      8       11
6 2024-03-31      9       13
7 2024-04-14     12       15
8 2024-04-28     10       17
9 2024-05-12     14       19

Note that the new week_num column contains the week number of the corresponding date in the date column of the DataFrame.

For example, we can see:

  • The date 2024-01-07 is week number 1 in the year.
  • The date 2024-01-21 is week number 3 in the year.
  • The date 2024-02-04 is week number 5 in the year.
  • The date 2024-02-18 is week number 7 in the year.

And so on.

It’s important to note the following details about how the %U operator calculates date numbers:

  • Sunday is considered the first day of the week.
  • All days in a new year that occur before the first Sunday are considered to be in week 0.

Keep these points in mind when interpreting the week numbers for each of the dates.

Example 2: Get Week Number and Year from Date in Pandas

Suppose we create the following pandas DataFrame that contains information about total sales made by some employee on various dates at some company:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'date': pd.date_range('1/1/2024', periods=10, freq='2M'),
                   'sales': [2, 5, 5, 4, 7, 8, 9, 12, 10, 14]})

#view DataFrame
print(df)

        date  sales
0 2024-01-31      2
1 2024-03-31      5
2 2024-05-31      5
3 2024-07-31      4
4 2024-09-30      7
5 2024-11-30      8
6 2025-01-31      9
7 2025-03-31     12
8 2025-05-31     10
9 2025-07-31     14

Suppose that we would like to extract the week number and year for each date in the date column of the DataFrame.

We can use the following syntax to do so:

#create new column that contains week number and year of date
df['week_num'] = df['date'].dt.strftime('%Y-%U')

#view updated DataFrame
print(df)

        date  sales week_num
0 2024-01-31      2  2024-04
1 2024-03-31      5  2024-13
2 2024-05-31      5  2024-21
3 2024-07-31      4  2024-30
4 2024-09-30      7  2024-39
5 2024-11-30      8  2024-47
6 2025-01-31      9  2025-04
7 2025-03-31     12  2025-13
8 2025-05-31     10  2025-21
9 2025-07-31     14  2025-30

Note that the new week_num column contains the year and week number of the corresponding date in the date column of the DataFrame.

For example, we can see:

  • The date 2024-01-31 is week number 4 in the year 2024.
  • The date 2024-03-31 is week number 13 in the year 2024.
  • The date 2024-05-31 is week number 21 in the year 2024.

And so on.

Additional Resources

The following tutorials explain how to perform other common tasks in Pandas:

Pandas: How to Combine Date and Time Columns
Pandas: How to Convert Epoch to Datetime
Pandas: How to Compare Dates

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *