Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/evidently/metrics/column_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,10 @@ def _calculate_value(self, dataset: Dataset):
column = dataset.column(self.metric.column)
try:
counts = column.data.value_counts()
if all(isinstance(c, bool) for c in self.metric.categories):
# only one boolean label is possible here
value = counts[self.metric.categories[0]] # type: ignore[index]
else:
value = 0
for cat in self.metric.categories:
if cat in counts:
value += counts[cat] # type: ignore[index]
value = 0
for cat in self.metric.categories:
if cat in counts:
value += counts[cat] # type: ignore[index]
except KeyError:
value = 0
total = column.data.count()
Expand Down
28 changes: 28 additions & 0 deletions tests/future/metrics/test_category_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pandas as pd
import pytest

from evidently.core.datasets import Dataset
from evidently.core.metric_types import CountValue
from evidently.core.report import Report
from evidently.metrics import CategoryCount


@pytest.mark.parametrize(
"data,categories,expected_count,expected_share",
[
(["a", "b", "c", "a"], ["a", "b"], 3, 0.75),
(["a", "b", "c", "a"], ["a", "z"], 2, 0.5),
([True, True, False, False], [True], 2, 0.5),
([True, True, False, False], [True, False], 4, 1.0),
([True, True, False, None], [False, True], 3, 1.0),
([True, True, True], [True, False], 3, 1.0),
],
)
def test_category_count_multiple_categories(data, categories, expected_count, expected_share):
dataset = Dataset.from_pandas(pd.DataFrame(data=dict(column=data)))
metric = CategoryCount("column", categories=categories)
snapshot = Report([metric]).run(dataset, None)
metric_result = snapshot._context.get_metric_result(metric.metric_id)
assert isinstance(metric_result, CountValue)
assert metric_result.count.value == expected_count
assert metric_result.share.value == pytest.approx(expected_share)