i have a dataframe with datetime index, i want to extract all the quarter ends
Assistant
To extract all quarter-end dates from a pandas DataFrame with a datetime index, you can use the pandas asfreq() method or filter using DatetimeIndex properties.
Here are a few clean and efficient ways to do it:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
✅ Method 1: Using resample('Q') and getting the last day of each quarter
quarter_ends = df.resample('Q').asfreq().index
⚠️ But this actually gives you the last business day of the quarter (depending on your data frequency). If you're looking for calendar quarter-ends (e.g., March 31, June 30, etc.), better to use:
✅ Method 2: Filtering the index directly
quarter_ends = df.index[df.index.is_quarter_end]
This is the most direct and readable way. It checks if each date in the index is the last day of a quarter (i.e. 31 Mar, 30 Jun, 30 Sep, 31 Dec).
🔹 Make sure your DataFrame index is a DatetimeIndex. You can check with:
print(type(df.index)) # Should be <class 'pandas.core.indexes.datetimes.DatetimeIndex'>