Python Pandas - Date Functionality



In time-series data analysis, especially in financial domains, date functionality plays a crucial role. Pandas provides robust tools to work with dates, allowing you to generate date sequences, manipulate date frequencies, and work with business days.

This tutorial will cover some of the essential date functionalities in Pandas, including generating sequences of dates, converting date series to different frequencies, and creating ranges of dates.

Pandas date functionality is divided into four primary concepts −

  • Date times: Represent specific points in time, like datetime.datetime from the standard library.

  • Time deltas: Represent duration in time, similar to datetime.timedelta.

  • Time spans: Define a span of time with a specific frequency, like months or years.

  • Date offsets: Represent relative time changes that respect calendar rules. Similar to dateutil.relativedelta.relativedelta from the dateutil package.

ConceptScalar ClassArray ClassPandas Data TypeCreation Method
Date timesTimestampDatetimeIndexdatetime64[ns]to_datetime() or date_range()
Time deltasTimedeltaTimedeltaIndextimedelta64[ns]to_timedelta() or timedelta_range()
Time spansPeriodPeriodIndexperiod[freq]Period() or period_range()
Date offsetsDateOffsetNoneNoneDateOffset

Generating a Sequence of Dates

You can create a range of dates using the date.range() function. By specifying the start date, number of periods, and frequency, you can generate a sequence of dates.

Example

The following example uses the date.range() function to generate a date range with a default frequency of one day ('D').

import pandas as pd

print(pd.date_range('1/1/2024', periods=5))

Its output is as follows −

DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04', '2024-01-05'],
   dtype='datetime64[ns]', freq='D')

Changing the Date Frequency

The frequency of a date range can be changed using the freq parameter in the pd.date_range() function. Pandas supports a variety of frequency options, allowing you to customize the intervals between dates.

Example

This example specifies the frequency 'M' to generates dates at the end of each month.

import pandas as pd

print(pd.date_range('1/1/2024', periods=5,freq='M'))

Its output is as follows −

DatetimeIndex(['2024-01-31', '2024-02-28', '2024-03-31', '2024-04-30', '2024-05-31'],
   dtype='datetime64[ns]', freq='M')

Working with Business Days

When analyzing financial data, it is common to exclude weekends and holidays. Pandas provides the bdate_range() function stands for business date ranges, which generates date ranges while excluding weekends. Unlike date_range(), it excludes Saturday and Sunday.

Example

This example uses the bdate_range() function to generate 10 working days.

import pandas as pd

print(pd.date_range('1/1/2024', periods=10))

Its output is as follows −

DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
               '2024-01-05', '2024-01-06', '2024-01-07', '2024-01-08',
               '2024-01-09', '2024-01-10'],
              dtype='datetime64[ns]', freq='D')

Observe that the output excludes the weekends (January 6th and 7th), and the range continues from the next business day, January 8th. Check your calendar for the days.

Using Offset Aliases

Pandas uses a set of predefined string aliases for common time series frequencies. These aliases, known as offset aliases, simplify the process of setting the frequency of your date ranges.

Commonly Used Offset Aliases

Below are the commonly used offset aliases in pandas.

Alias Description Alias Description
B business day frequency BQS business quarter start frequency
D calendar day frequency A annual(Year) end frequency
W weekly frequency BA business year end frequency
M month end frequency BAS business year start frequency
SM semi-month end frequency BH business hour frequency
BM business month end frequency H hourly frequency
MS month start frequency T, min minutely frequency
SMS SMS semi month start frequency S secondly frequency
BMS business month start frequency L, ms milliseconds
Q quarter end frequency U, us microseconds
BQ business quarter end frequency N nanoseconds
QS quarter start frequency

For example, using the alias 'B' with pd.date_range() creates a date range with only business days, while 'M' sets the frequency to the end of the month.

Advertisements