Write a program in Python to print the day of the year in a given date series

PythonPandasServer Side ProgrammingProgramming

Input

Assume, you have a series and find the day of the year from a given specific range of dates.

Solution

To solve this, we will follow the below approaches.

  • Define a Series

  • Set date_range as ‘2020-01-10’ with five periods. It is defined below,

pd.date_range('2020-01-10',periods=5)
  • Find the day using Series.dt.dayofyear.

Example

Let us see the complete implementation to get a better understanding −

import pandas as pd
date = pd.date_range('2020-01-10',periods=5)
data = pd.Series(date)
print(data.dt.dayofyear)

Output

0    10
1    11
2    12
3    13
4    14
raja
Updated on 24-Feb-2021 06:49:11

Advertisements