Creating every month Gini index from 20 yrs of daily data

I am trying to find gini index for every month of data which has around 20 years of daily data, is there any way to do the same in one code so that I get gini index for every month by converting data?


This, and many other similar tasks, are best done using pandas. Supposing you have a DataFrame df (perhaps read in via pd.read_csv()) with a column date, containing your dates as timestamps, datetimes, etc., and a column gini with your Gini values as a numeric type, e.g.

import pandas as pd
df = pd.DataFrame({ 
    "date": [pd.Timestamp("2022-03-01"), pd.Timestamp("2022-03-02"), pd.Timestamp("2022-04-01")],
    "gini": [1, 2, 3]})

then you can get the monthly mean gini index by using groupby on a PeriodIndex of the months:

df.groupby(pd.PeriodIndex(df["date"], freq="M"))["gini"].mean()

which gives you

2022-03    1.5
2022-04    3.0
Freq: M, Name: gini, dtype: float64