Python Pandas - Extract the ordinal day of year from the DateTimeIndex with specific time series frequency


To extract the ordinal day of year from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.dayofyear property

At first, import the required libraries −

import pandas as pd

Create a DatetimeIndex with period 6 and frequency as D i.e. day −

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D')

Display DateTimeIndex −

print("DateTimeIndex...\n", datetimeindex)

Get the day of year −

print("\nGet the ordinal day of year..\n",datetimeindex.dayofyear)

Example

Following is the code −

import pandas as pd

# DatetimeIndex with period 6 and frequency as D i.e. day
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# get the day of year
print("\nGet the ordinal day of year..\n",datetimeindex.dayofyear)

Output

This will produce the following code −

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-21 02:30:50+11:00',
'2021-10-22 02:30:50+11:00', '2021-10-23 02:30:50+11:00',
'2021-10-24 02:30:50+11:00', '2021-10-25 02:30:50+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='D')
DateTimeIndex frequency...
<Day>

Get the ordinal day of year..
Int64Index([293, 294, 295, 296, 297, 298], dtype='int64')

Updated on: 18-Oct-2021

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements