I have the following hash and I would like to group by year/month and sum the values:
range = {
"2020-11-06 00:00:00 +0000" => 234100.14176395803,
"2020-11-07 00:00:00 +0000" => 57731.63072153537,
"2020-11-08 00:00:00 +0000" => 68903.8902730576,
"2020-11-09 00:00:00 +0000" => 180971.98008691627,
........
"2021-02-01 00:00:00 +0000" => 169004.96299567595,
"2021-02-02 00:00:00 +0000" => 183363.9687217272,
"2021-02-03 00:00:00 +0000" => 200400.2505103338
}
I was able to do the group: hash = Hash[range.collect { |k,v| [k.to_date.strftime("%Y-%m"), v]}]
but I'm unable to get the values sum, the value results and getting with the hash collect is only the last day.
{
"2020-11" => 155346.83103528828,
"2020-12" => 175424.41029800082,
"2021-01" => 55366.44438577791,
"2021-02" => 200400.2505103338
}
Using reduce method hash.values.reduce(:+)
I can get the total values but I need them by year by month.