Find the longest streak of strictly increasing months in a pandas time series
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Given a `pd.Series` of floats indexed by an ascending monthly `DatetimeIndex` (month-end timestamps), implement `growth_streak_report(s)` that finds the longest run of consecutive strictly increasing periods and returns a one-row summary. `start` is the timestamp of the last period before the run began (its base) and `end` is the timestamp of the final increasing period.
The function must meet these requirements:
- Return one row with a 0..0 `RangeIndex` and the columns `streak_length` (`int64`), `start` and `end` (`datetime64[ns]`), and `start_value`, `end_value` and `total_growth_pct` (`float64`).
- `streak_length` counts increases (steps), not points. Increases must be strictly greater: equal values break the run.
- `total_growth_pct` is `end_value / start_value - 1`, rounded to 4 decimal places.
- If two runs share the maximum length, report the earliest.
- With no increase at all, `streak_length` is 0, `start` and `end` are `NaT`, and the float columns are `NaN`, keeping the single row and all dtypes.
Example:
```python
idx = pd.to_datetime([
"2023-01-31", "2023-02-28", "2023-03-31",
"2023-04-30", "2023-05-31", "2023-06-30",
])
s = pd.Series([10.0, 12.0, 11.0, 13.0, 15.0, 16.0], index=idx)
growth_streak_report(s)
# Longest run: 2023-03-31 (11.0) -> 2023-06-30 (16.0), 3 increases
# start = 2023-03-31 (base before the run), end = 2023-06-30
# streak_length = 3, start_value = 11.0, end_value = 16.0
# total_growth_pct = 16.0/11.0 - 1 = 0.4545
```
The first run grows once (10 to 12). The drop to 11 makes 2023-03-31 a new base, and 11, 13, 15, 16 give three increases in a row, the longest run.
```hint One boolean per step
Turn the series into one True or False per period (did it beat the previous period?). The task then becomes finding the longest block of consecutive True values.
```
```hint Where a run starts
The base of a run is the period before its first increase. Check which timestamp and value you report as `start`.
```
### Constraints & Assumptions
- The index is sorted ascending with month-end timestamps.
- The series has at least 2 points and no NaNs.
### Clarifying Questions to Ask
- If a month is missing from the index, should the gap break the streak or count as one step?
- What should `total_growth_pct` be when the base value is 0?
### What a Strong Answer Covers
- Building a strict period-over-period increase mask, so equal values break a run.
- Measuring run length in steps and choosing the earliest run among ties.
- Reporting the base before the first increase as `start`, with the matching values and the rounded growth.
- Returning one row with the exact dtypes, including the no-increase case with `NaT` and `NaN`.
### Follow-up Questions
- How would you also report all runs tied for the maximum length instead of just the earliest?
- How would you adapt this to allow a tolerance so tiny dips don't break a streak?
- How would you produce this report for every product in a long table of monthly revenue?
Overview: Write a pandas function that finds the longest run of strictly increasing months in a time series and returns a one-row summary of its start, end, values and total growth. It tests run-length logic with diff and cumsum, tie-breaking, and keeping dtypes in the no-growth case.