Skip to content

Commit

Permalink
Check for categorical data to histogram (#5540)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSkovMadsen authored Jan 11, 2023
1 parent 22d6205 commit 733af92
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions holoviews/operation/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,8 @@ def _process(self, element, key=None):
hist_range = (0, 1)
steps = self.p.num_bins + 1
start, end = hist_range
if isinstance(start, str) or isinstance(end, str) or isinstance(steps, str):
raise ValueError("Categorical data found. Cannot create histogram from categorical data.")
if is_datetime:
start, end = dt_to_int(start, 'ns'), dt_to_int(end, 'ns')
if self.p.log:
Expand Down
7 changes: 7 additions & 0 deletions holoviews/tests/operation/test_operation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime as dt
from unittest import skipIf
import pytest

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -256,6 +257,12 @@ def test_histogram_operation_pd_period(self):
hist = Histogram(hist_data, kdims='Date', vdims=('Date_frequency', 'Frequency'))
self.assertEqual(op_hist, hist)

def test_histogram_categorical(self):
series = Dataset(pd.Series(['A', 'B', 'C']))
kwargs = {'bin_range': ('A', 'C'), 'normed': False, 'cumulative': False, 'num_bins': 3}
with pytest.raises(ValueError):
histogram(series, **kwargs)

def test_points_histogram_weighted(self):
points = Points([float(i) for i in range(10)])
op_hist = histogram(points, num_bins=3, weight_dimension='y', normed=True)
Expand Down

0 comments on commit 733af92

Please sign in to comment.