When a dataset has a meaningful index, idxmax answers “which labeled record contains the largest value?” rather than returning only the maximum itself. On a Series, it returns the index of the highest value; on a DataFrame indexed by dates, it can return the date associated with each column’s maximum. section 3.17 section 1.5
Finding the date of a maximum
Load market-price data with date and value columns, set date as the index, and call df.idxmax(). The returned label identifies when the maximum value occurred. Compare this with a boolean mask such as df.loc[df['value'] == df['value'].max(), ['date', 'value']], which explicitly selects matching rows. For both minimum and maximum labels in one operation, use df.agg(['idxmin', 'idxmax']). section 3.17
df.set_index('date').idxmin()
df.set_index('date').idxmax()
Inspect the concise DataFrame pattern that turns date labels into the index before retrieving the minimum and maximum labels. section 3.17
Use idxmax when the label matters as much as the value:
- Use s.idxmax() to identify the month or other index label containing a student’s highest score. section 1.5
- Set a descriptive column as the index before calling idxmax when the returned label should be a date or other identifier. section 3.17
- Use a boolean mask instead when you need to select every row matching the maximum value. section 3.17 section 1.5
