I like to move it move it with deque

Hey there, 👋

How is your Python going?

In this Mathspp Insider 🐍🚀 email we’ll talk about moving averages, deque, and pandas!

This email at a glance

  • I’m a child;

  • a great example use case for collections.deque is to compute moving averages; and

  • learn pandas!

Do you want to learn pandas?

I don’t know a lot about pandas, but what I learned, I learned from Matt.

I bought his book “Effective Pandas” a couple of years ago and today Matt is launching his book “Effective Pandas 2”, with lots of updates and new sections on pandas 2, PyArrow, how to test and debug pandas code, etc.

I bought a copy for myself right before sending this email and because today is launch day, you can use the code PD20 to get 20% off of the book and his other books, courses, and more.

As far as I can tell, the code is only valid today.

You can also check his “Effective Pandas” bundle or browse all of this books and courses on pandas, Python, and related concepts, here.

Foreword

First of all, I must tell you that the subject of this email only makes sense when read at the rythm of this song: https://www.youtube.com/watch?v=hdcTmpvDO0I.

Moving average

A moving average is something you compute on a series of values.

When computing a regular average, you sum all values and divide by the total number of values.

For a moving average, the input is still a series of values but you also return a series of values.

To compute the moving average, you pick a value n – to which you generally call the “window size” – you add the first n values, and then you divide by n to compute the first value of the result.

Something like sum(input_data[:n]) / n.

To compute the next value, you sum n values again, but this time you use the values at indices 1, 2, ..., n.

Something like sum(input_data[1:n+1]) / n.

And you keep doing this until you reach the end of the data.

This is used in finance, for example.

Implementation

So, how do you implement a function moving_average making use of a deque?

The deque is the perfect data structure for this computation because of its parameter maxlen.

(If you don’t know what I’m talking about, you can check this deque tutorial.)

So, pause this email and try to implement moving_average yourself.

After you’re done, email me your code!

This is the code I wrote:

def moving_average(data, window_size):
    values = []
    window = deque(maxlen=window_size)
    for element in data:
        window.append(element)
        values.append(sum(window) / len(window))

    return values

The star of the code is the deque and all of the work it’s doing behind the scenes when you call window.append(element).

If the deque is already full, adding a new element to the window will pop the leftmost element.

This provides a pretty natural way to “slide over” the input data without too much trouble.

And it’s much more efficient than computing successive slices.

As for the return value, your code might have dropped the initial elements.

I’ve seen moving averages computed in all sorts of different ways, perhaps the most standard one would’ve been to write return values[n - 1:].

ICYMI

🐍🚀 How was this email?

Login or Subscribe to participate in polls.