def get_pies(df_m, df_m_trend): """Get pies info""" return { "all": serie_to_dict(to_percentages(df_m.mean())), "month": serie_to_dict(to_percentages(df_m.tail(1).T.iloc[:, 0])), "year": serie_to_dict(to_percentages(df_m.iloc[-12:].mean())), }
def get_year_data(dfi): df = dfi.pivot_table(values=c.COL_PAGES, index=c.COL_DATE, columns=c.COL_LANGUAGE, aggfunc="sum") df = to_year_start(df) out = {x: serie_to_dict(df[x]) for x in df.columns} out["Total"] = serie_to_dict(to_year_start(dfi)[c.COL_PAGES]) return out
def get_month_data(dfi): out = { i: serie_to_dict(dfa.resample("MS")[c.COL_PAGES].sum()) for i, dfa in dfi.set_index(c.COL_DATE).groupby(c.COL_LANGUAGE) } out["Total"] = serie_to_dict( dfi.set_index(c.COL_DATE).resample("MS")[c.COL_PAGES].sum()) for name, data in { **out }.items(): # The ** is to avoid problems while mutating the dict serie = smooth_serie(pd.Series(data)) out[f"{name}_trend"] = serie_to_dict(serie[6:]) return out
def get_dashboard(dfi): out = serie_to_dict(dfi.groupby(c.COL_LANGUAGE)[c.COL_PAGES].sum()) out["Total"] = int(dfi[c.COL_PAGES].sum()) out["Years"] = int(dfi[c.COL_DATE].dt.year.nunique()) return out
def get_top(dfi, groupby, top_n=20): df = (dfi.groupby(groupby).agg({ c.COL_PAGES: "sum" }).sort_values(c.COL_PAGES, ascending=False)[:top_n]) return serie_to_dict(df[c.COL_PAGES])
def get_year_percent(data, cumsum=True): df = pd.DataFrame(data) if cumsum: df = df.cumsum() # Get percentatges df = 100 * df.div(df["Total"], axis=0).fillna(0) return {x: serie_to_dict(df[x]) for x in df.columns if x != "Total"}
def extract_data(vdp, df, export=False): """Extract data from the dataframe""" calendars = read_calendars() df_w_trend = df.resample("W-MON").sum().apply(smooth_serie) df_m = df.resample("MS").sum() df_m_trend = df_m.apply(smooth_serie) to_dict_reversed = lambda df: serie_to_dict() out = { "week_trend": serie_to_dict(df_w_trend), "month": serie_to_dict(df_m), "month_percent": serie_to_dict(to_percentages(df_m)), "month_trend": serie_to_dict(df_m_trend), "month_trend_percent": serie_to_dict(to_percentages(df_m_trend)), "pies": get_pies(df_m, df_m_trend), "colors": {name: data["color"] for name, data in calendars.items()}, } out["cards"] = get_cards(out, calendars) if export: vdp.write_yaml(out, f"I will fail") return out