5.3 C
London
Saturday, December 28, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Get Day of Year from Date

Pandas: How to Get Day of Year from Date

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to get the day of year from a date column in a pandas DataFrame:

df['day_of_year'] = df['date'].dt.dayofyear

This particular example creates a new column called day_of_year that contains the day of the year of the value in the date column.

Note that the values for day_of_year will range from 1 (January 1st) to 365 (December 31st).

The following example shows how to use this syntax in practice.

Example: Get Day of Year from Date in Pandas

Suppose we have the following pandas DataFrame that contains information about the total sales made at some store on various dates:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'date': pd.date_range(start='1/1/2022', freq='M', periods=10),
                   'sales': [6, 8, 10, 5, 4, 8, 8, 3, 5, 14]})

#view DataFrame
print(df)

         date  sales
0  2022-01-31      6
1  2022-02-28      8
2  2022-03-31     10
3  2022-04-30      5
4  2022-05-31      4
5  2022-06-30      8
6  2022-07-31      8
7  2022-08-31      3
8  2022-09-30      5
9  2022-10-31     14

Related: How to Create a Date Range in Pandas

We can use the following code to create a new column called day_of_year that contains the day of the year from the date column:

#create new column that contains day of year in 'date' column
df['day_of_year'] = df['date'].dt.dayofyear

#view updated DataFrame
print(df)

        date  sales  day_of_year
0 2022-01-31      6           31
1 2022-02-28      8           59
2 2022-03-31     10           90
3 2022-04-30      5          120
4 2022-05-31      4          151
5 2022-06-30      8          181
6 2022-07-31      8          212
7 2022-08-31      3          243
8 2022-09-30      5          273
9 2022-10-31     14          304

The new column called day_of_year contains the day of year from the date column.

It’s worth noting that if you’re working with a leap year, this function will automatically extend the range of possible values from 365 to 366.

Also note that if the column you’re working with is a string column, you must first use pd.to_datetime() to convert the strings to recognizable dates:

#convert string column to datetime and calculate day of year
df['day_of_year'] = pd.to_datetime(df['date']).dt.dayofyear

Note: You can find the complete documentation for the pandas dayofyear function here.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Add and Subtract Days from a Date in Pandas
How to Select Rows Between Two Dates in Pandas
How to Create Date Column from Year, Month and Day in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories