1

I am trying to calculate and plot moving average along with the data it is calculated from:

def movingAvg(df):
window_size = 7
i = 0

moving_averages = []

while i < len(df) - window_size + 1:
    current_window = df[i : i + window_size]
    window_average = current_window.mean()
    moving_averages.append(window_average)
    i += 1

return moving_averages

    
dates = df_valid['dateTime']
startDay = dates.iloc[0]
lastDay = dates.iloc[-1]

fig, ax = plt.subplots(figsize=(20, 10))
ax.autoscale()
#plt.xlim(startDay, lastDay)

df_valid.sedentaryActivityMins.reset_index(drop=True, inplace=True)
df_moving = pd.DataFrame(movingAvg(df_valid['sedentaryActivityMins']))

df_nan = [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]
df_nan = pd.DataFrame(df_nan)

df_moving = pd.concat([df_nan, df_moving])
plt.plot(df_valid.sedentaryActivityMins)
plt.plot(df_moving)

#plt.show()

But as the moving average uses 7 windows, the list of moving averages is 7 items short, and therefore the plots do not follow each other correctly.

I tried putting 7 "NaN" into the moving average list, but those are ignored when I plot.

The plot is as follows: here

But I would like the the orange line to start 7 steps ahead. So it looks like this: enter image description here

df_valid.sedentaryActivityMins.head(40)
0     608
1     494
2     579
3     586
4     404
5     750
6     573
7     466
8     389
9     604
10    351
11    553
12    768
13    572
14    616
15    522
16    675
17    607
18    229
19    529
20    746
21    646
22    625
23    590
24    572
25    462
26    708
27    662
28    649
29    626
30    485
31    509
32    561
33    664
34    517
35    587
36    602
37    601
38    495
39    352
Name: sedentaryActivityMins, dtype: int64

Any ideas as to how? Thanks in advance!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
MyTivoli
  • 125
  • 2
  • 11
  • NaNs cannot be shown in plot. You can either make the first seven values as zero or assign it to the series itself inorder to ger them plotted – Raghul Raj Aug 05 '20 at 18:35
  • Hi! If I assign them to 0, the plot looks really funky. How do I assign it to the series? – MyTivoli Aug 05 '20 at 18:36
  • I'd recommend using a dynamic window. Until the 7th point, the window size should increase gradually from 1 to 7 and stay constant after that. – Raghul Raj Aug 05 '20 at 18:39
  • What do you expect those values to be? – wwii Aug 05 '20 at 18:39
  • I expect the first 7 to not be shown, otherwise the two plots follow each other incorrectly – MyTivoli Aug 05 '20 at 18:40
  • 1
    Can you add some data to your question? Please read [mre]. – wwii Aug 05 '20 at 18:43
  • Related: [Moving average produces array of different length?](https://stackoverflow.com/questions/47484899/moving-average-produces-array-of-different-length), – wwii Aug 05 '20 at 19:00

1 Answers1

2

When you do a concat, the indexes don't change. The NaNs will also take the same indices as the first 7 observations of your series. So either do a reset index after the concat or set ignore_index as True as follows:

df_moving = pd.concat([df_nan, df_moving],ignore_index=True)
plt.plot(x)
plt.plot(df_moving)

This gives the output as expected:

enter image description here

Raghul Raj
  • 1,428
  • 9
  • 24