Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas - weekly line graph from yearly data

i have a yearly dataset with energy consumption readings in 30min intervals:

                      data
2012-11-01 00:00:00  0.177
2012-11-01 00:30:00  0.141
2012-11-01 01:00:00  0.112
2012-11-01 01:30:00  0.082
2012-11-01 02:00:00  0.080
...

how do i plot a multiple line graph showing the data consumption for each week? i.e. eventually i will have a graph with 52 lines, where the x axis is the time in the week (days? half days? hours?) and the y axis is the consumption.

thanks

like image 764
Noam Naveh Avatar asked Nov 15 '25 01:11

Noam Naveh


1 Answers

Consider the dataframe df with index tidx

tidx = pd.date_range('2016-01-01', '2017-01-01', freq='30T')
df = pd.DataFrame(dict(data=(np.random.randn(len(tidx)) + .01).cumsum()), tidx)

Create time deltas relative to first date

deltas = df.index - df.index[0]

Create weeks with respect to deltas

week = deltas.days // 7

Build new pd.Series object with a pd.MultiIndex

d1 = pd.Series(
    df.data.values,
    [deltas - pd.to_timedelta(week, 'w'), week]
)

unstack

# d2 = d1.unstack().add_prefix('Week ') # working version

# explanation version
d2 = print(d1.unstack().add_prefix('Week '))
d2.iloc[:10, :5]

            Week 0    Week 1     Week 2    Week 3     Week 4
00:00:00 -0.973634 -5.350765   6.918354 -3.536488  22.489763
00:30:00 -2.320088 -5.632370   6.670572 -4.852697  24.493568
01:00:00 -2.499885 -3.458980   8.748229 -4.059241  25.278759
01:30:00 -3.525366 -2.286180   8.345489 -5.241154  26.086324
02:00:00 -2.110594 -2.801211   8.626546 -6.840205  28.028737
02:30:00 -2.811840 -2.605900   9.224140 -6.601106  28.014257
03:00:00 -4.119560 -3.497173   9.801411 -6.431539  29.284452
03:30:00 -4.927063 -3.406615  11.729483 -5.526467  27.834364
04:00:00 -5.573758 -2.559643  13.653698 -3.948048  28.956422
04:30:00 -4.878375 -4.322923  12.017081 -2.862244  28.364504

All Together

tidx = pd.date_range('2016-01-01', '2017-01-01', freq='30T')
df = pd.DataFrame(dict(data=(np.random.randn(len(tidx)) + .01).cumsum()), tidx)

deltas = df.index - df.index[0]

week = deltas.days // 7

d1 = pd.Series(
    df.data.values,
    [deltas - pd.to_timedelta(week, 'w'), week]
)

d2 = d1.unstack().add_prefix('Week ')

ax = d2.plot(rot=30, colormap='jet')
lg = ax.legend(ncol=4, loc=2, bbox_to_anchor=(1.05, 1))

enter image description here

like image 141
piRSquared Avatar answered Nov 17 '25 22:11

piRSquared