Skip to content

Chronological lagged rolling features

Question

Given a pandas DataFrame with unique asset/date pairs and a return column, compute each asset's previous three-observation mean, excluding the current row.

Short answer

Sort by asset and date, reject duplicate keys, then shift within each asset before rolling. Requiring three observations leaves the first three rows per asset unavailable. The mean is over observations, not necessarily three consecutive calendar days.

Original question and answer written by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question

Start with a free lesson: Basic Statistics and Regression is part of a free topic. Create a free account, no card needed, to read it in full with worked solutions.

Read it free