Skip to content

Count every boolean category in CategoryCount - #1940

Open
breken-ai wants to merge 1 commit into
evidentlyai:mainfrom
breken-ai:fix/category-count-both-bools
Open

breken-ai wants to merge 1 commit into
evidentlyai:mainfrom
breken-ai:fix/category-count-both-bools

Conversation

@breken-ai

Copy link
Copy Markdown

What was wrong

CategoryCountCalculation._calculate_value has a special branch for when every category is a boolean:

if all(isinstance(c, bool) for c in self.metric.categories):
    #  only one boolean label is possible here
    value = counts[self.metric.categories[0]]

CategoryCount accepts several categories, so categories=[True, False] is valid, but that branch counts only the first one:

df = pd.DataFrame({"b": [True, True, False, False]})
Report([CategoryCount(column="b", categories=[True, False])]).run(Dataset.from_pandas(df))
# before: {'count': 2.0, 'share': 0.5}
# after:  {'count': 4.0, 'share': 1.0}

Fix

Remove the boolean branch. The loop below it already looks up each label with cat in counts / counts[cat], which works for boolean labels too. A boolean label on a 0/1 integer column still counts 0, as before.

Tests

  • New tests/future/metrics/test_category_count.py covers multiple string categories (including a missing one) and boolean categories. [True, False] and [False, True] with a None fail on main (count 2 instead of 4, and 1 instead of 3). All 6 cases pass with the fix.
  • The broader suite (tests/calculations tests/future tests/metrics tests/metric_preset tests/test_preset tests/test_suite tests/stattests) has the same 71 pre-existing failures as main (all No module named 'IPython', because I did not install the jupyter extra), and no new ones.
  • ruff check and ruff format --check pass on the changed files.

Side note: the existing tests/future/metrics/category_count.py is not collected by pytest because of its file name, and it fails on main (it compares CountValue.count to an int). I left it unchanged.

This PR was written with an AI coding agent (Claude Code, run by breken-ai), and I checked the reproduction and the tests above.

CategoryCount(categories=[True, False]) only counted the first category, because the all-boolean branch read counts[categories[0]]. The per-category loop already handles boolean labels, so use it for every case.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant