Every Pandas Function You Can (Should) Use to Manipulate Time Series

Original Source Here

3.2 Percentage changes

Another common metric that can be derived from time-series data is day-to-day percentage change:

To calculate day-to-day percentage change, shift one period forward and divide the original distribution by the shifted one and subtract 1. The resulting values are given as proportions of what they were the day before.

Since it is a common operation, Pandas implements it with the pct_change function:

4. Resampling

Often, you may want to increase or decrease the granularity of time series to generate new insights. These operations are called resampling or changing the frequency of time series, and we will discuss the Pandas functions related to them in this section.

4.1 Changing the frequency with asfreq

The second dataset does not have a fixed date frequency, i.e., the period difference between each date is not the same:


Let’s fix this by giving it a calendar day frequency (daily):


We just made the frequency of the date more granular. As a result, new dates were added, leading to more missing values. You can now interpolate them using any of the techniques we discussed earlier.

You can see the list of built-in frequency aliases from here. A more interesting scenario would be using custom frequencies:

# 10 month frequency
data2.asfreq("10M", method="bfill").head(7)

There is also a reindex function that operates similarly and supports additional missing value filling logic. We won’t discuss it here as there are better options we will consider.


Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: