Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve error messages for unit conversion #13005

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 1 commit into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,8 +1486,11 @@ def convert_units(self, x):

if self.converter is None:
return x

ret = self.converter.convert(x, self.units, self)
try:
ret = self.converter.convert(x, self.units, self)
except Exception as e:
raise munits.ConversionError('Failed to convert value(s) to axis '
f'units: {x!r}') from e
return ret

def set_units(self, u):
Expand Down
26 changes: 16 additions & 10 deletions lib/matplotlib/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,30 @@
class StrCategoryConverter(units.ConversionInterface):
@staticmethod
def convert(value, unit, axis):
"""Converts strings in value to floats using
"""Convert strings in value to floats using
Copy link
Contributor

Choose a reason for hiding this comment

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

Just inherit the docstring from units.ConversionInterface perhaps? (and possibly edit that docstring).

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we can reasonably use an inherited docstring here. Using the conversion interface for string-index mapping is a bit of a stretch and justifies explicit description.

  • type of value is a string, which is rather the exception for ConversionInterface
  • mark unit as UnitData
  • note that axis is unused.

Copy link
Contributor

Choose a reason for hiding this comment

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

sure

mapping information store in the unit object.

Parameters
----------
value : string or iterable
value or list of values to be converted
unit : :class:`.UnitData`
object string unit information for value
axis : :class:`~matplotlib.Axis.axis`
axis on which the converted value is plotted
Value or list of values to be converted.
unit : `.UnitData`
An object mapping strings to integers.
axis : `~matplotlib.axis.Axis`
axis on which the converted value is plotted.

.. note:: *axis* is unused.

Returns
-------
mapped_ value : float or ndarray[float]

.. note:: axis is not used in this function
mapped_value : float or ndarray[float]
"""
if unit is None:
raise ValueError(
'Missing category information for StrCategoryConverter; '
'this might be caused by unintendedly mixing categorical and '
'numeric data')

# dtype = object preserves numerical pass throughs
values = np.atleast_1d(np.array(value, dtype=object))

Expand Down Expand Up @@ -190,7 +196,7 @@ def update(self, data):
self._mapping[val] = next(self._counter)


# Connects the convertor to matplotlib
# Register the converter with Matplotlib's unit framework
units.registry[str] = StrCategoryConverter()
units.registry[np.str_] = StrCategoryConverter()
units.registry[bytes] = StrCategoryConverter()
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def default_units(x, axis):
from matplotlib import cbook


class ConversionError(TypeError):
pass


class AxisInfo(object):
"""
Information to support default axis labeling, tick labeling, and
Expand Down