The code below calculates the mean','median','max','min'
of the vals
values that are in correlation to the dates month_changes
. The code calculates the mean','median','max','min'
by separating the intervals in years. I want to implement a bit to the code where it adds zeroes to the mean','median','max','min'
values for the years that start from the starting_year
variable, also to the years that are in between one another like 2020 in this example it will also add zeroes. How would I be able to do that?
import numpy as np
import pandas as pd
month_changes = np.array(["2018-04-01 00:00:00", "2018-05-01 00:00:00", "2019-03-01 00:00:00", "2019-04-01 00:00:00","2019-08-01 00:00:00", "2019-11-01 00:00:00", "2019-12-01 00:00:00","2021-01-01 00:00:00"])
vals = np.array([10, 23, 45, 4,5,12,4,-6])
starting_year = 2016
def YearlyIntervals(vals):
data = pd.DataFrame({"Date": month_changes, "Averages": vals})
data["Date"] = pd.to_datetime(data["Date"])
out=(data.groupby(data["Date"].dt.year)
.agg(['mean','median','max','min'])
.droplevel(0,1)
.rename(columns=lambda x:'Average' if x=='mean' else x.title())
)
return out
PnL_YearlyFilter= YearlyIntervals(vals)
Output
Average Median Max Min
Date
2018 16.5 16.5 23 10
2019 14.0 5.0 45 4
2021 -6.0 -6.0 -6 -6
Expected Output
Average Median Max Min
Date
2016 0 0 0 0
2017 0 0 0 0
2018 16.5 16.5 23 10
2019 14.0 5.0 45 4
2020 0 0 0 0
2021 -6.0 -6.0 -6 -6