pythonintermediate
Cumulative Max & Streak Detection
Detect streaks, new highs, and consecutive-day patterns in time-series using cummax and groupby.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import numpy as np
df = pd.DataFrame({'date': pd.date_range('2024-01-01', periods=30),'value': np.random.randint(0, 100, 30)})
df['all_time_high'] = df['value'] >= df['value'].cummax()
# Winning streak (consecutive positives)
df['pos'] = (df['value'] > df['value'].shift(1)).astype(int)
df['streak'] = df['pos'] * (df['pos'].groupby((df['pos'] != df['pos'].shift()).cumsum()).cumcount() + 1)
print(df[['date','value','all_time_high','streak']].head(15))Use Cases
- sports analytics
- trading signals
- performance streaks
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Pandas Time Series Analysis
Time series operations with resampling, rolling windows, date offsets, and period conversions.
Best for: Sales trend analysis with moving averages
#pandas#time-series
pythonintermediate
Pandas Rolling & Expanding Windows
Compute moving averages, rolling sums, and cumulative stats on time-series data with pandas.
Best for: sales forecasting
#pandas#time-series
pythonbeginner
Pandas Time-Series Resampling
Resample time-series data from daily to weekly/monthly frequencies with aggregation functions.
Best for: time-series analytics
#pandas#time-series
pythonintermediate
Pandas merge_asof for Time-Based Joins
Perform an as-of join to match events to the most recent reference record within a time window.
Best for: tick data joins
#pandas#merge-asof