-
-
Notifications
You must be signed in to change notification settings - Fork 404
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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.' | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
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: | ||
|
There was a problem hiding this comment.
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'