I have lovely code that makes a list of averages of all entries on a given timeindex period. For example, on a dataframe with ten years of data, it will return the average values for each day of the week. What I want to do is propagate these average values back to the entire dataframe in as few lines as possible.
I'm reasonably proud of the code to make the averages. For example, take a dataframe that has this data (I am listing "day of week" for your convenience since this value is implicit in the datetimeindex and this column does not exist in my own data):
Datetimeindex day of week value
2019-01-01 3 60
2019-01-02 4 46
2019-01-03 5 72
2019-01-04 6 16
2019-01-05 7 94
2019-01-06 1 100
2019-01-07 2 41
2019-01-08 3 63
2019-01-09 4 3
2019-01-10 5 52
2019-01-11 6 38
2019-01-12 7 43
2019-01-13 1 22
2019-01-14 2 73
2019-01-15 3 72
2019-01-16 4 30
2019-01-17 5 52
2019-01-18 6 25
2019-01-19 7 28
2019-01-20 1 83
2019-01-21 2 98
2019-01-22 3 9
2019-01-23 4 61
2019-01-24 5 88
2019-01-25 6 54
2019-01-26 7 32
2019-01-27 1 32
2019-01-28 2 41
2019-01-29 3 65
2019-01-30 4 24
2019-01-31 5 92
2019-02-01 6 76
2019-02-02 7 47
2019-02-03 1 80
2019-02-04 2 62
My code goes like this:
result = time_series.groupby(time_series.index.weekday).mean()
This provides me with a len(7) vector of averages for each day of the week that looks like this:
day of week mean
1 63.4
2 63
3 53.8
4 32.8
5 71.2
6 41.8
7 48.8
What I would simply like to do is reverse the formula and paste the averages back into the original source dataframe. Taking the example above, the desired result would be:
Datetimeindex value
2019-01-01 53.8
2019-01-02 32.8
2019-01-03 71.2
2019-01-04 41.8
2019-01-05 48.8
2019-01-06 63.4
2019-01-07 63
2019-01-08 52.56
2019-01-09 30.16
2019-01-10 71.04
2019-01-11 46.96
2019-01-12 39.76
2019-01-13 63.4
2019-01-14 63
2019-01-15 52.56
2019-01-16 30.16
2019-01-17 71.04
2019-01-18 46.96
2019-01-19 39.76
2019-01-20 63.4
2019-01-21 63
2019-01-22 52.56
2019-01-23 30.16
2019-01-24 71.04
2019-01-25 46.96
2019-01-26 39.76
2019-01-27 63.4
2019-01-28 63
2019-01-29 52.56
2019-01-30 30.16
2019-01-31 71.04
2019-02-01 46.96
2019-02-02 39.76
2019-02-03 63.4
2019-02-04 63
It feels like it should be possible to simply reverse the original formula and say:
time_series.groupby(time_series.index.weekday) = result
But that clearly doesn't work. Or even if it was possible to perform the mean() process in place, such as:
result = time_series.groupby(time_series.index.weekday).mean(inplace=True)
or
result.loc[result.groupby(by=val)] = time_series.groupby(by=val).mean().values
but neither work. I'd love to do this in a one-liner and I feel so close, but I simply can't make it work. Do you have any ideas?