Skip to content

fix(gain_loss): 'Boolean Series key will be reindexed' warnings when df has rows outside p1/p2 #588

Open

Description

@murray-ds

Noticed while using the package.

What: GainLoss emits six UserWarning: Boolean Series key will be reindexed to match DataFrame index. whenever df has rows that are in neither period. That is the normal case when the data covers more than the two periods being compared. The results are correct, so the warnings are only noise.

Where (main @ c5f4b2a): gain_loss.py:172 subsets df to p1_index | p2_index. L179-195 then index that subset with the original full-length masks, so pandas reindexes each one.

Repro (pandas 2.3.3):

import pandas as pd
from openretailscience.analysis.gain_loss import GainLoss

df = pd.DataFrame({
    "customer_id": [1, 1, 2, 2, 3, 3, 4, 5],
    "brand": ["Coke", "Pepsi", "Coke", "Coke", "Pepsi", "Coke", "Pepsi", "Coke"],
    "period": ["2023", "2024", "2023", "2024", "2023", "2024", "2024", "2022"],
    "unit_spend": [10.0, 12.0, 8.0, 5.0, 7.0, 9.0, 4.0, 6.0],
})
GainLoss(
    df,
    p1_index=df["period"] == "2023",
    p2_index=df["period"] == "2024",
    focus_group_index=df["brand"] == "Coke",
    focus_group_name="Coke",
    comparison_group_index=df["brand"] == "Pepsi",
    comparison_group_name="Pepsi",
)
gain_loss.py:179: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  df[focus_group_index & p1_index].groupby(grp_cols, observed=False)[value_col].agg(agg_func),
... (same at lines 180, 181, 191, 192, 193)

Expected: no warnings. For example, align the masks to the subset first (mask.loc[df.index]), or compute the group/period masks before subsetting.

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