Pandas DatetimeIndex Usage Explained

Pandas DatetimeIndex makes it easier to work with Date and Time data in our DataFrame. DatetimeIndex() can contain metadata related to date and timestamp and is a great way to deal with DateTime related data and do the calculations on data and time.

1. DatetimeIndex() Syntax and Usage

Pandas DatetimeIndex() takes multiple parameters, below is the Syntax of the DatetimeIndex(). Let’s take a look at the parameters of the DatetimeIndex by examples.


pandas.DatetimeIndex(data=None, freq=_NoDefault.no_default, tz=None, normalize=False, closed=None, ambiguous='raise', dayfirst=False, yearfirst=False, dtype=None, copy=False, name=None)

Following is the list of parameters that you can pass to DateTimeIndex.

  • data — Datetime-like data.
  • freq — Stands for frequency. Check all the Possible values for the freq here.
  • tz — This keyword stands for timezone and sets the timezone of the data
  • normalize — It is used to set the start/end dates to midnight before generating the date range
  • closed — Can be set to “left” or “right”. Used for setting the boundary dates it should be included or not.
  • dayfirst — It takes a boolean value. If True, parse dates in data with the day’s first order.
  • yearfirst — It takes a boolean value as well. If True parse dates in data with the year first order.
  • copy — Use this keyword if you want to Make a copy of input ndarray.
  • name — the value you want to store in the index.

2. Attributes of Pandas DatetimeIndex

The DateTime Index has a lot of attributes for finding different properties of the DatetimeIndex from pandas DataFrame. See the complete list with the examples below.

2.1 Extract Year from Pandas DatetimeIndex

The Pandas Datetimeindex has the attribute “year” attribute, which let us get the year of the DatetimeIndex. See the below code where we get the year of DatetimeIndex. We use the Datetimeindex.year attribute.


import pandas as pd
dt_index_obj = pd.DatetimeIndex(['2017-12-31 16:00:00-08:00', '2017-12-31 17:00:00-08:00',
               '2017-12-31 18:00:00-08:00'],
              dtype='datetime64[ns, US/Pacific]', freq='H')

# get years of the dateTimeIndex
dt_index_obj.year

# Output
# Int64Index([2017, 2017, 2017], dtype='int64')


Related Article: Pandas Filter DataFrame Rows on Dates

2.2 Extract Month from Pandas DatetimeIndex

To get the month of the DatetimeIndex, use the DatetimeIndex.month attribute. DatetimeIndex.month is an attribute that returns the month of the DateTimeIndex object. See the following example.


# get months of the dateTimeIndex
dt_index_obj.month

# Output
# Int64Index([12, 12, 12], dtype='int64')

2.3 Extract Day, Hour, Minute, and Time from DatetimeIndex

In the following example, you will see how we get the day, hour, minute, time, week, date, and more from the DatetimeIndex object.


# get day of the dateTimeIndex
dt_index_obj.day

# get hour of the dateTimeIndex
dt_index_obj.hour

# get minute of the dateTimeIndex
dt_index_obj.minute

# get date of the dateTimeIndex
dt_index_obj.date

# get time of the dateTimeIndex
dt_index_obj.time

# get timezone of the dateTimeIndex
dt_index_obj.timetz

# get week of the dateTimeIndex
dt_index_obj.week

# get month name of the dateTimeIndex
dt_index_obj.month_name

3. Find Date is First Day of the Month from Pandas DatatimeIndex

Let’s say you want to do calculations on data that contain the first day of the month, you can get this by using the Pandas datetimeinedex.is_month_start attribute. datetimeindex.is_month_start indicates whether the date is the first day of the month. It returns true if a day is the first day of the month otherwise it will return false.


# find if the date is first day of the month
dt_index_obj.is_month_start

# Output
# array([ True, False, False])

4. Find Date is the End of the Month 

Datetimeindex.is_month_end attribute is useful when you want to find the end of the month. For example, do some calculation on those data that comes at the end of a month. See the following code example.

In the following example, an array is the returned value that returns true if the value of the corresponding date is the last day of the month.


# find if the date is the end of the month
dt_index_obj.is_month_end

# Output
# array([False,  True,  True])

5. Find Day is the Start Day of the Year 

To find out if a day is the start of the year in pandas DatetimeIndex, use the DatetimeIndex.is_year_start. It will return true if the day is the start of the year otherwise it will return a false value.

See the following example where we use the is_year_start of pandas DatetimeIndex .


# find day is the start day of the year
dt_index_obj.is_year_start

# Output
# array([ True, False, False])

6. Find Day is the End of the Year

The recommended way to find out if a day is the end of the year in pandas DatetimeIndex, use the DatetimeIndex.is_year_end. It will return true if the day is the last day of the year otherwise it will return a false value.


# find day is the end of the year
dt_index_obj.is_year_end

# Output
# array([False,  True,  True])

7. Find Year is a Leap in Pandas DatetimeIndex 

In cases where you want to find the leap years out of all the dates in pandas DatetimeIndex we use the DatetimeIndex.is_leap_year. It returns a boolean value of true if the year is a leap year otherwise it will return false.


# find year is a leap year
dt_index_obj.is_leap_year

# Output
# array([ True, False, False])

8. Find Day of Week DatetimeIndex

Monday will be considered is the zeroth Day and the last day of the week will be Sunday which is the seventh day of the week. DatetimemIndex.dayofweek which returns the day number of the week.

See the following example.


# find day of the week
dt_index_obj.dayofweek

# Output
# Int64Index([2, 5, 6], dtype='int64')

9. Round Date in Pandas DatetimeIndex

We can also round the date in a DatetimeIndex, to do so we have to use the Pandas Datetimeindex.round() function. It returns a rounded date. In the following example, we have passed a parameter of ‘H’, which stands for hours and tells the function to round the date by hours.

See the following example and look at the output. The date is rounded by Hours.


# round date
dt_index_obj.round('H')

# Output
# DatetimeIndex(['2020-01-01 16:00:00-08:00', '2017-12-30 17:00:00-08:00',
#                '2017-12-31 18:00:00-08:00'],
#               dtype='datetime64[ns, US/Pacific]', freq=None)

10. Conclusion

In this article, you have learned about Pandas DatetimeIndex, its syntax, usage, and different attributes. Please feel free to ask questions if you have any or leave a comment of appreciation.

AlixaProDev

I am a software Engineer with extensive 4+ years of experience in Programming related content Creation.

Leave a Reply

You are currently viewing Pandas DatetimeIndex Usage Explained