Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

histogram op: raise error on non numerical data #5481

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 17 additions & 1 deletion holoviews/operation/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..core.data.util import dask_array_module
from ..core.util import (
group_sanitizer, label_sanitizer, datetime_types, isfinite,
dt_to_int, isdatetime, is_dask_array, is_cupy_array, is_ibis_expr
dt_to_int, isdatetime, is_dask_array, is_cupy_array, is_ibis_expr, is_number
)
from ..element.chart import Histogram, Scatter
from ..element.raster import Image, RGB
Expand Down Expand Up @@ -700,6 +700,14 @@ class histogram(Operation):
Used for setting a common style for histograms in a HoloMap or AdjointLayout.""")

def _process(self, element, key=None):

bad_bins_msg = 'bins only accept numerical data.'
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this message could be improved a bit. How about:

'Histogram binning can only be performed on numerical data'

if (isinstance(self.p.bins, np.ndarray) and
(self.p.bins.dtype.kind in 'SU' or self.bins.dtype.kind == 'O' and not isdatetime(self.bins))):
raise TypeError(bad_bins_msg)
elif isinstance(self.p.bins, (list, tuple)) and any(not is_number(bin) for bin in self.p.bins):
raise TypeError(bad_bins_msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Given all these boolean expressions, it would be good to pull them out onto separate lines for clarity.


if self.p.groupby:
if not isinstance(element, Dataset):
raise ValueError('Cannot use histogram groupby on non-Dataset Element')
Expand All @@ -716,8 +724,16 @@ def _process(self, element, key=None):

if hasattr(element, 'interface'):
data = element.interface.values(element, selected_dim, compute=False)
dtype = element.interface.dtype(element, selected_dim)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is dtype not easily available with compute=False?

else:
data = element.dimension_values(selected_dim)
dtype = getattr(data, 'dtype', None)

if dtype and (dtype.kind in 'SU' or dtype.kind == 'O' and not isdatetime(data)):
raise TypeError(
'The histogram operation can only be applied to a numerical dimension.'
)


is_datetime = isdatetime(data)
if is_datetime:
Expand Down