-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
update units.ConversionInterface to have to_numeric
and from_numeric
#16922
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
base: main
Are you sure you want to change the base?
update units.ConversionInterface to have to_numeric
and from_numeric
#16922
Conversation
5ccae25
to
80087a2
Compare
So this is a major overhaul that might create deprecation issues with the third party packages the conversation interface is aimed at. Is there buy in/packages that need this functionality? And can it be achieved w/o renaming convert? |
80087a2
to
17c0e26
Compare
Thanks for the quick response, in this PR as proposed I have marked Regarding the buy in, I am not sure, but I believe this change is needed because I believe my issue is related to pandas-dev/pandas#18344, and now that I have prepared this PR, I will be trying to make sure the tests pass, and testing my code with a patch to pandas that implements a Regarding the name change, I believe this could be made without a name change, but it sacrifices the readability and makes the functions slightly ambiguous. Arguably you could make the methods be |
17c0e26
to
408ecb6
Compare
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.
Thanks @terencehonles. This is rather a big step in the API, which will have to be thought through carefully. Please be prepared, that the review process may take some time. While I haven't looked into the details yet, the basic idea looks reasonable.
Apart from the individual comments, please address the following:
- Change all deprecations to 3.3, because 3.2 is already out.
- Make all deprecations pending. While you wrote you did in the PR comment, I don't see it in the code.
- This needs an API change note at
doc/api/next_api_changes.rst
for the deprecations. The new to and from methods should also be announced, probably best atdoc/users/next_whats_new
.
@@ -51,7 +51,7 @@ def default_units(x, axis): | |||
from matplotlib import cbook | |||
|
|||
|
|||
class ConversionError(TypeError): | |||
class ConversionError(ValueError): |
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.
This would be an API change. Not sure it's needed or even desirable. It's raised when convert_units(x)
cannot process the argument. Typically that should happen rather if x has an unsupported type than value.
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.
Looking at where it is used
matplotlib/lib/matplotlib/axis.py
Lines 1466 to 1470 in c30129b
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 |
I personally would have thought that should have been a ValueError
, but I can understand why in some cases you can argue one way or another for ValueError
vs TypeError
. I do agree this would be a breaking change if someone is expecting to catch a TypeError
instead of a ConversionError
and maybe you feel strongly enough that this shouldn't change. This is partially changed because I'm more under the impression it should be a ValueError
, and I did end up using the exception a couple more places and throwing this instead of a ValueError
. The appropriate fix may be creating two different errors or maybe just documenting an API change.
So in summary: I did this because it didn't seem like it would be a big deal and I am using the exception more widely than it was used before (new use cases with the new method I added, and for existing errors that were both ValueError
s and seemed like they should have actually fallen under a ConversionError
)
tests.py
Outdated
@@ -1,29 +0,0 @@ | |||
#!/usr/bin/env python |
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.
Please do not add unrelated changes to large PRs like this one.
Apart from whether this should be removed or not, any unrelated change makes reviewing harder, which may delay the review process, or even discourage reviewers from taking on the PR.
Instead, please keep the PR as focused as possible and submit everything else as a separate PR.
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.
These changes were simply to allow me to more easily test my changes (other than using docker, I only have Python 2.7 and 3.8 on my system). I created PR #16986 which cherry picks these changes.
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.
Sorry, I guess this comment more belongs on tox.ini the change on this file was that I wasted a lot of time trying to figure why this test failed, and I didn't realize there was a diff file that was generated. I updated this test util to make it more obvious, but I can also pull this out to a different PR
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.
Actually this comment is on a deleted file, I will pull this and the change I was talking about ^^^ into a different PR.
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.
It also looks like I was wrong about deleting this file. It looks like it expects you have already installed matplotlib in pip editable mode, and I'll suggest that change instead of deleting this file (I looked at its commit log and it has been updated recently so I looked a bit closer)
408ecb6
to
e170f99
Compare
This change updates `matplotlib.units.ConversionInterface` to have a `to_numeric` instead of a `convert` method, and adds a `from_numeric` method. The method `convert` is renamed to `to_numeric` and similarly named methods are updated accordingly. The renaming is done to make `convert` less ambiguous with a forward and backward conversion on the `ConversionInterface`. Adding a `from_numeric` method standardizes the way to convert back to a native object from an internal numeric value. This allows registered converters to provide more specific conversions which will allow formatters and locators to provide an implementation which is not as tied to the internal values Matplotlib uses.
e170f99
to
2fe4da5
Compare
There have been lots of discussion about fixing units in Matplotlib, including the problem of calculating the inverse. Currently my understanding is that there is a new data model under development that will address this by @tacaswell and @story645. How does this PR fit into those plans? I’m also not in favour of renaming a long-standing method for purely aesthetic reasons. |
I'm not aware of those plans, I can possibly help with those plans if needed. I was mainly taking a stab at this because it looks like it has been a longstanding issue and it affects me directly. So rather than just patching it internally or updating |
I updated the deprecations, the I appreciate you looking at it and @jklymak and any others that might have the chance to offer some input. |
I'm sure that would be appreciated. https://github.com/matplotlib/matplotlib/labels/units Note that there are other solutions to pandas-dev/pandas#18344 under https://github.com/matplotlib/matplotlib/labels/Date%20handling, in particular #15008 |
The data model work is going to intersect with the units work by better managing the life cycle of when it gets called, but should be relatively agnostic to how we spell the unit conversion. |
We talked about this on the dev-call this week (.https://hackmd.io/-fJ2hZcJQz2gfbqzabrZMQ?view#Units-change). We have several concerns:
|
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.
Please:
- revert the rename API changes, we can discuss those in a separate PR
- address concerns about converters that are not actually invertable.
Since this Pull Request has not been updated in 60 days, it has been marked "inactive." This does not mean that it will be closed, though it may be moved to a "Draft" state. This helps maintainers prioritize their reviewing efforts. You can pick the PR back up anytime - please ping us if you need a review or guidance to move the PR forward! If you do not plan on continuing the work, please let us know so that we can either find someone to take the PR over, or close it. |
This change updates
matplotlib.units.ConversionInterface
to have ato_numeric
instead of aconvert
method, and adds afrom_numeric
method. The methodconvert
is renamed toto_numeric
and similarly named methods are updated accordingly. The renaming is done to makeconvert
less ambiguous with a forward and backward conversion on theConversionInterface
.Adding a
from_numeric
method standardizes the way to convert back to a native object from an internal numeric value. This allows registered converters to provide more specific conversions which will allow formatters and locators to provide an implementation which is not as tied to the internal values Matplotlib uses.PR Summary
PR Checklist