This is an automated email from the ASF dual-hosted git repository.
rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new 2c8665f9cbc fix(histogram): normalize before accumulating a cumulative
histogram (#44652)
2c8665f9cbc is described below
commit 2c8665f9cbcb440e93cef110750c5839d3aa4ea8
Author: Suliman Abdulrazzaq
<[email protected]>
AuthorDate: Sun Oct 11 20:20:36 2026 +0300
fix(histogram): normalize before accumulating a cumulative histogram
(#44652)
---
superset/utils/pandas_postprocessing/histogram.py | 8 ++++--
.../pandas_postprocessing/test_histogram.py | 32 ++++++++++------------
2 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/superset/utils/pandas_postprocessing/histogram.py
b/superset/utils/pandas_postprocessing/histogram.py
index 28375207ae6..25d389d6952 100644
--- a/superset/utils/pandas_postprocessing/histogram.py
+++ b/superset/utils/pandas_postprocessing/histogram.py
@@ -91,8 +91,7 @@ def histogram(
def hist_values(series: Series) -> np.ndarray:
# we might have NaN values as the result of grouping so we need to
drop them
- result = np.histogram(series.dropna(), bins=bin_edges)[0]
- return result if not cumulative else np.cumsum(result)
+ return np.histogram(series.dropna(), bins=bin_edges)[0]
if len(groupby) == 0:
# without grouping
@@ -107,9 +106,14 @@ def histogram(
)
histogram_df.columns = bin_edges_str
+ # normalize by the total count of data points before accumulating, so that
a
+ # cumulative histogram shows the share of data points up to each bin
if normalize:
histogram_df = histogram_df / histogram_df.values.sum()
+ if cumulative:
+ histogram_df = histogram_df.cumsum(axis=1)
+
# reorder the columns to have the groupby columns first
histogram_df = histogram_df.reset_index().loc[:, groupby + bin_edges_str]
diff --git a/tests/unit_tests/pandas_postprocessing/test_histogram.py
b/tests/unit_tests/pandas_postprocessing/test_histogram.py
index bfc702a71bb..474c412f032 100644
--- a/tests/unit_tests/pandas_postprocessing/test_histogram.py
+++ b/tests/unit_tests/pandas_postprocessing/test_histogram.py
@@ -103,24 +103,20 @@ def
test_histogram_with_groupby_and_cumulative_and_normalize():
"6.4 - 8.2",
"8.2 - 10.0",
]
- assert result.values.tolist() == [
- [
- "A",
- 0.06666666666666667,
- 0.06666666666666667,
- 0.13333333333333333,
- 0.13333333333333333,
- 0.2,
- ],
- [
- "B",
- 0.0,
- 0.06666666666666667,
- 0.06666666666666667,
- 0.13333333333333333,
- 0.13333333333333333,
- ],
- ]
+ # each bin holds the share of all data points that are in the group and up
to
+ # that bin
+ assert result["group"].tolist() == ["A", "B"]
+ assert result.iloc[0, 1:].tolist() == pytest.approx([0.2, 0.2, 0.4, 0.4,
0.6])
+ assert result.iloc[1, 1:].tolist() == pytest.approx([0.0, 0.2, 0.2, 0.4,
0.4])
+
+
+def test_histogram_cumulative_and_normalize_ends_at_one():
+ data_with_no_groupings = DataFrame({"a": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})
+ result = histogram(
+ data_with_no_groupings, "a", [], bins, cumulative=True, normalize=True
+ )
+ assert result.shape == (1, bins)
+ assert result.values.tolist()[0] == pytest.approx([0.2, 0.4, 0.6, 0.8,
1.0])
def test_histogram_with_non_numeric_column():