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 first day of the quarter
To check whether the date in DateTimeIndex is the first day of the quarter, use the DateTimeIndex.is_quarter_start property. This property returns a boolean array indicating which dates fall on the first day of their respective quarters.
Understanding Quarters
In Pandas, a year is divided into four quarters ?
- Quarter 1: January 1 to March 31
- Quarter 2: April 1 to June 30
- Quarter 3: July 1 to September 30
- Quarter 4: October 1 to December 31
Syntax
DateTimeIndex.is_quarter_start
Example
Let's create a DateTimeIndex and check which dates are the first day of a quarter ?
import pandas as pd
# Create DatetimeIndex with period 6 and frequency of 30 days
datetimeindex = pd.date_range('2021-10-1 02:30:50', periods=6, tz='Australia/Adelaide', freq='30D')
# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
# Check whether the date in DateTimeIndex is the first day of the quarter
print("\nCheck whether the date in DateTimeIndex is the first day of the quarter...")
print(datetimeindex.is_quarter_start)
DateTimeIndex...
DatetimeIndex(['2021-10-01 02:30:50+09:30', '2021-10-31 02:30:50+10:30',
'2021-11-30 02:30:50+10:30', '2021-12-30 02:30:50+10:30',
'2022-01-29 02:30:50+10:30', '2022-02-28 02:30:50+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='30D')
DateTimeIndex frequency...
<30 * Days>
Check whether the date in DateTimeIndex is the first day of the quarter...
[ True False False False False False]
Example with Specific Quarter Start Dates
Let's create another example with dates that include multiple quarter start dates ?
import pandas as pd
# Create DatetimeIndex with specific dates including quarter starts
dates = ['2023-01-01', '2023-04-01', '2023-07-01', '2023-10-01', '2023-11-15']
datetimeindex = pd.DatetimeIndex(dates)
print("DateTimeIndex...")
print(datetimeindex)
print("\nIs quarter start...")
print(datetimeindex.is_quarter_start)
# Show the actual dates that are quarter starts
quarter_starts = datetimeindex[datetimeindex.is_quarter_start]
print("\nDates that are quarter starts:")
print(quarter_starts)
DateTimeIndex...
DatetimeIndex(['2023-01-01', '2023-04-01', '2023-07-01', '2023-10-01',
'2023-11-15'],
dtype='datetime64[ns]', freq=None)
Is quarter start...
[ True True True True False]
Dates that are quarter starts:
DatetimeIndex(['2023-01-01', '2023-04-01', '2023-07-01', '2023-10-01'], dtype='datetime64[ns]', freq=None)
Key Points
- Returns a boolean array with
Truefor quarter start dates - Quarter starts are always the 1st day of January, April, July, and October
- Works with timezone-aware DateTimeIndex objects
- Useful for financial and business analytics where quarterly reporting is common
Conclusion
The is_quarter_start property provides an efficient way to identify quarter start dates in a DateTimeIndex. This is particularly useful for financial analysis and quarterly business reporting where you need to filter or group data by quarter boundaries.
