Unexpected Treemap aggreation

I’m trying to create a treemap similar to this one in the docs, but it isn’t working as I’d expect. Here is a minimum working example, build_hierarchical_dataframe has been slightly modified to work with newer versions of pandas.

import pandas as pd
import plotly.graph_objects as go

grouped = pd.DataFrame.from_dict({'i': {1: 795, 2: 804, 3: 804, 4: 842, 5: 842}, 'k': {1: '100110', 2: '100110', 3: '100190', 4: '100110', 5: '100190'}, 'v': {1: 34496492.0, 2: 145861590.0, 3: 2773245499.0, 4: 837272860.0, 5: 5846843779.0}, 'q': {1: 169185.0, 2: 774015.8809999999, 3: 17436469.7, 4: 2986917.31, 5: 26319328.719}})

def build_hierarchical_dataframe(df, levels, value_column, color_columns=None):
    """
    Build a hierarchy of levels for Sunburst or Treemap charts.

    Levels are given starting from the bottom to the top of the hierarchy,
    ie the last level corresponds to the root.
    """
    df_all_trees = []

    for i, level in enumerate(levels):
        df_tree = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
        dfg = df.groupby(levels[i:]).sum()
        dfg = dfg.reset_index()
        df_tree['id'] = dfg[level].copy()
        if i < len(levels) - 1:
            df_tree['parent'] = dfg[levels[i+1]].copy()
        else:
            df_tree['parent'] = 'total'
        df_tree['value'] = dfg[value_column]
        df_tree['color'] = dfg[color_columns[0]] / dfg[color_columns[1]]
        df_all_trees.append(df_tree)
    total = pd.DataFrame.from_dict(dict(id='total', parent='',
                              value=df[value_column].sum(),
                              color=df[color_columns[0]].sum() / df[color_columns[1]].sum(), index=[0]))
    df_all_trees.append(total)
    # df_all_trees= pd.DataFrame(df_all_trees)
    return pd.concat(df_all_trees, ignore_index=True).drop('index',axis=1)

df_all_trees = build_hierarchical_dataframe(grouped, ['i', 'k'], 'v', ['v','q'])


fig= go.Figure(go.Treemap(
    labels=df_all_trees['id'],
    parents=df_all_trees['parent'],
    values=df_all_trees['value'],
    branchvalues='total',
    hovertemplate='<b>%{label} </b> <br> Sales: %{value}<br> Success rate: %{color:.2f}',
    name=''
    ),)
fig.show()

The issue with this plot is two-fold:

  1. At the root only one box is shown for 100110

  2. When we click on 100110 we can see additional boxes, with values larger than the box shown at the root.