Skip to content

Improve categorical converter error message #17824

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

Merged
merged 2 commits into from
Jul 5, 2020
Merged
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
9 changes: 9 additions & 0 deletions lib/matplotlib/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def convert(value, unit, axis):
'Missing category information for StrCategoryConverter; '
'this might be caused by unintendedly mixing categorical and '
'numeric data')
StrCategoryConverter._validate_unit(unit)
# dtype = object preserves numerical pass throughs
values = np.atleast_1d(np.array(value, dtype=object))
# pass through sequence of non binary numbers
Expand Down Expand Up @@ -80,6 +81,7 @@ def axisinfo(unit, axis):

.. note: axis is not used
"""
StrCategoryConverter._validate_unit(unit)
# locator and formatter take mapping dict because
# args need to be pass by reference for updates
majloc = StrCategoryLocator(unit._mapping)
Expand Down Expand Up @@ -109,6 +111,13 @@ def default_units(data, axis):
axis.units.update(data)
return axis.units

@staticmethod
def _validate_unit(unit):
if not hasattr(unit, '_mapping'):
raise ValueError(
f'Provided unit "{unit}" is not valid for a categorical '
'converter, as it does not have a _mapping attribute.')


class StrCategoryLocator(ticker.Locator):
"""Tick at every integer mapping of the string data."""
Expand Down