Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas - Indicate whether the date in DateTimeIndex is the last day of the quarter
To check whether the date in DateTimeIndex is the last day of the quarter, use the DateTimeIndex.is_quarter_end property. This property returns a boolean array indicating which dates fall on the last day of their respective quarters.
Understanding Quarter Divisions
Before using is_quarter_end, it's important to understand how pandas divides the year into quarters ?
- Quarter 1: January 1st to March 31st
- Quarter 2: April 1st to June 30th
- Quarter 3: July 1st to September 30th
- Quarter 4: October 1st to December 31st
Basic Example
Let's create a DateTimeIndex and check which dates are quarter-end dates ?
import pandas as pd
# Create a DatetimeIndex with period 6 and frequency as 15 days
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-6-15 02:30:50', periods=6, tz='Australia/Adelaide', freq='15D')
# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
# Check whether the date in DateTimeIndex is the last day of the quarter
print("\nCheck whether the date in DateTimeIndex is the last day of the quarter...")
print(datetimeindex.is_quarter_end)
DateTimeIndex...
DatetimeIndex(['2021-06-15 02:30:50+09:30', '2021-06-30 02:30:50+09:30',
'2021-07-15 02:30:50+09:30', '2021-07-30 02:30:50+09:30',
'2021-08-14 02:30:50+09:30', '2021-08-29 02:30:50+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='15D')
DateTimeIndex frequency...
<15 * Days>
Check whether the date in DateTimeIndex is the last day of the quarter...
[False True False False False False]
Example with Quarter-End Dates
Let's create a more focused example using actual quarter-end dates ?
import pandas as pd
# Create specific dates including quarter-end dates
dates = ['2021-03-31', '2021-04-01', '2021-06-30', '2021-09-30', '2021-12-31']
datetime_index = pd.DatetimeIndex(dates)
print("DateTimeIndex with quarter-end dates...")
print(datetime_index)
print("\nIs quarter end?")
print(datetime_index.is_quarter_end)
# Create a DataFrame to see results more clearly
df = pd.DataFrame({
'Date': datetime_index,
'Is_Quarter_End': datetime_index.is_quarter_end
})
print("\nResults in DataFrame format:")
print(df)
DateTimeIndex with quarter-end dates...
DatetimeIndex(['2021-03-31', '2021-04-01', '2021-06-30', '2021-09-30',
'2021-12-31'],
dtype='datetime64[ns]', freq=None)
Is quarter end?
[ True False True True True]
Results in DataFrame format:
Date Is_Quarter_End
0 2021-03-31 True
1 2021-04-01 False
2 2021-06-30 True
3 2021-09-30 True
4 2021-12-31 True
Filtering Quarter-End Dates
You can use the is_quarter_end property to filter and extract only quarter-end dates ?
import pandas as pd
# Create a date range covering multiple quarters
date_range = pd.date_range('2021-01-01', '2021-12-31', freq='M')
print("Monthly dates for 2021:")
print(date_range)
# Filter only quarter-end dates
quarter_end_dates = date_range[date_range.is_quarter_end]
print("\nQuarter-end dates only:")
print(quarter_end_dates)
Monthly dates for 2021:
DatetimeIndex(['2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30',
'2021-05-31', '2021-06-30', '2021-07-31', '2021-08-31',
'2021-09-30', '2021-10-31', '2021-11-30', '2021-12-31'],
dtype='datetime64[ns]', freq='M')
Quarter-end dates only:
DatetimeIndex(['2021-03-31', '2021-06-30', '2021-09-30', '2021-12-31'], dtype='datetime64[ns]', freq=None)
Conclusion
The is_quarter_end property is useful for financial analysis and time series operations where quarterly periods matter. It returns True for dates that fall on March 31st, June 30th, September 30th, and December 31st of any year.
