Skip to content

fix(revenue_tree): a single group loses its group label #602

Open

Description

@murray-ds

What: RevenueTree with group_col drops the group label when the data has exactly one group. The result gets a RangeIndex instead of store=Downtown. Checked on main @ c5f4b2a.

Cause: revenue_tree.py:70-73 in calc_tree_kpis resets the index when len(df.index) == period_count. That condition is meant to detect "no group_col", but one group also aggregates to two rows. The same reset fires with two groups when one is missing from a period. The decision should rest on whether group_col is set, not on the row count.

Failing test:

import pandas as pd

from openretailscience.analysis.revenue_tree import RevenueTree
from openretailscience.options import ColumnHelper

cols = ColumnHelper()


class TestRevenueTreeGroupLabels:
    def test_single_group_keeps_its_label(self):
        """A single-store analysis (two aggregated rows) is still indexed by store."""
        downtown = pd.DataFrame(
            {
                cols.customer_id: [101, 102, 101],
                cols.transaction_id: [9001, 9002, 9004],
                cols.unit_spend: [20.50, 30.00, 40.00],
                "quarter": ["2024Q1", "2024Q1", "2024Q2"],
                "store": ["Downtown", "Downtown", "Downtown"],
            },
        )
        result = RevenueTree(
            df=downtown, period_col="quarter", p1_value="2024Q1", p2_value="2024Q2", group_col="store"
        ).df
        assert result.index.name == "store"
        assert list(result.index) == ["Downtown"]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions