Instruction: Given a time series dataset in a DataFrame, demonstrate how to implement a custom resampling operation that calculates the weighted average of the data points, with weights decreasing exponentially with time.
Context: This question tests the candidate's ability to manipulate time series data, their understanding of advanced indexing and resampling techniques in Pandas, and their skill in applying custom operations to grouped data.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
First, let me clarify the task at hand. We're given a time series dataset encapsulated in a Pandas DataFrame. Our objective is to resample this dataset — perhaps changing its granularity, say from minutes to hours — and to compute a weighted average for each resampled bin. The twist is in the weighting: each data point's weight decreases exponentially as it gets older, emphasizing more recent data in our average.
To tackle this, we assume we have a DataFrame named df with a DateTime index and a column values from which we want to calculate the weighted average. We also assume our DataFrame is sorted by the DateTime index, which is a best practice when working with time series data....