Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions superset/utils/pandas_postprocessing/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def histogram(
if groupby is None:
groupby = []

# drop empty values from the target column

Copilot AI Nov 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment says "drop empty values" but the code actually drops NULL/NaN values. Consider using more precise terminology: "Drop NULL values from the target column" to be clearer about what's being removed.

Suggested change
# drop empty values from the target column
# drop NULL/NaN values from the target column

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but optional in my mind.

df = df.dropna(subset=[column])
if df.empty:
return df
Comment thread
janani-gurram marked this conversation as resolved.

# convert to numeric, coercing errors to NaN
df[column] = to_numeric(df[column], errors="coerce")

Expand Down
67 changes: 67 additions & 0 deletions tests/unit_tests/pandas_postprocessing/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,70 @@ def test_histogram_with_some_non_numeric_values():
histogram(data_with_non_numeric, "a", ["group"], bins)
except ValueError as e:
assert str(e) == "Column 'group' contains non-numeric values" # noqa: PT017


def test_histogram_with_groupby_and_some_null_values():
data_with_groupby_and_some_nulls = DataFrame(
{
"group": ["A", "A", "B", "B", "A", "A", "B", "B", "A", "A"],
"a": [1, 2, 3, 4, 5, None, 7, 8, 9, 10],
"b": [1, 2, 3, 4, 5, None, 7, 8, 9, 10],
}
)

result = histogram(data_with_groupby_and_some_nulls, "a", ["group"], bins)
assert result.shape == (2, bins + 1)
assert result.columns.tolist() == [
"group",
"1.0 - 2.8",
"2.8 - 4.6",
"4.6 - 6.4",
"6.4 - 8.2",
"8.2 - 10.0",
]
assert result.values.tolist() == [["A", 2, 0, 1, 0, 2], ["B", 0, 2, 0, 2, 0]]


def test_histogram_with_no_groupby_and_some_null_values():
data_with_no_groupby_and_some_nulls = DataFrame(
{
"a": [1, 2, 3, 4, 5, None, 7, 8, 9, 10],
"b": [1, 2, 3, 4, 5, None, 7, 8, 9, 10],
}
)

result = histogram(data_with_no_groupby_and_some_nulls, "a", [], bins)
assert result.shape == (1, bins)
assert result.columns.tolist() == [
"1.0 - 2.8",
"2.8 - 4.6",
"4.6 - 6.4",
"6.4 - 8.2",
"8.2 - 10.0",
]
assert result.values.tolist() == [[2, 2, 1, 2, 2]]


def test_histogram_with_groupby_and_all_null_values():
data_with_groupby_and_all_nulls = DataFrame(
{
"group": ["A", "A", "B", "B", "A", "A", "B", "B", "A", "A"],
"a": [None, None, None, None, None, None, None, None, None, None],
"b": [None, None, None, None, None, None, None, None, None, None],
}
)

result = histogram(data_with_groupby_and_all_nulls, "a", ["group"], bins)
assert result.empty


def test_histogram_with_no_groupby_and_all_null_values():
data_with_no_groupby_and_all_nulls = DataFrame(
{
"a": [None, None, None, None, None, None, None, None, None, None],
"b": [None, None, None, None, None, None, None, None, None, None],
}
)

result = histogram(data_with_no_groupby_and_all_nulls, "a", [], bins)
assert result.empty
Loading