Extract Day from DatetimeIndex in Python Pandas



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: 2021-10-19T08:48:19+05:30

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements