
- Python Pandas Tutorial
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
- Python Pandas Useful Resources
- Python Pandas - Quick Guide
- Python Pandas - Cheatsheet
- Python Pandas - Useful Resources
- Python Pandas - Discussion
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.
Concept | Scalar Class | Array Class | Pandas Data Type | Creation Method |
---|---|---|---|---|
Date times | Timestamp | DatetimeIndex | datetime64[ns] | to_datetime() or date_range() |
Time deltas | Timedelta | TimedeltaIndex | timedelta64[ns] | to_timedelta() or timedelta_range() |
Time spans | Period | PeriodIndex | period[freq] | Period() or period_range() |
Date offsets | DateOffset | None | None | DateOffset |
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.