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


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

At first, import the required libraries −

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:35:55', periods=6, tz='Australia/Sydney',freq='D')

Display DateTimeIndex −

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

Get the day −

print("\nGetting the day..\n",datetimeindex.day)

Example

Following is the code −

import pandas as pd

# DatetimeIndex with period 6 and frequency as D i.e. day
# timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:35:55', 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
print("\nGetting the day..\n",datetimeindex.day)

Output

This will produce the following output −

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

Getting the day..
   Int64Index([20, 21, 22, 23, 24, 25], dtype='int64')

Updated on: 19-Oct-2021

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements