Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
POC: Add validation of converters to formatters#25766
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 ourterms of service andprivacy 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?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
The idea of "just see if you can convert a datetime" doesn't work because the JPL units don't takein datetime objects, but rather their own units (in hindsight, this is actually pretty obvious)... they just output something that is compatible with the DateFormatters... I also think that warning on actually compatible combinations is still rather unacceptable, while less bad than erroring. axisinfo is actually a pretty interesting way of resolving it... my original thought was to somehow make it so thatany converter that provides axisinfo of a particular formatter would work, but that breaks down with the inheritance structure of mpl.dates formatters.... but if we instead do this only for date converters (and of course, other formatters could do similar things): defvalidate_converter(self,converter):ifnotisinstance(converter.axisinfo(None,None).majfmt, (DateFormatter,AutoDateFormatter)):_api.warn_external(f"Expected a DateConverter for DateFormatter, got{type(converter)}" ) Modulo perhaps some tweaks like paying attention to either minor or major formatter, or the error message text, this actually passes tests, including JPL units related tests. But it still relies on passing insomething to the "unit" field of
Adding a call to So I've gone down a number of paths here and it is feeling like a bit of a stalemate, not sure how to draw the lines to include only the things we want to include, and feeling like perhaps an assertive runtime check is not necessisarily what we actually want. |
Okay I've gone down the path of using axis info, though caching the value in a private variable instead of recomputing from the converter in the validator. Axis info is passed every time the validator is called, though of course a given validator may ignore it. In addition, a new field has been added to AxisInfo, called As for behavior of downstream:
diff --git a/pandas/plotting/_matplotlib/converter.py b/pandas/plotting/_matplotlib/converter.pyindex 9b0fe99e2d..3b8adaddaf 100644--- a/pandas/plotting/_matplotlib/converter.py+++ b/pandas/plotting/_matplotlib/converter.py@@ -337,7 +337,8 @@ class DatetimeConverter(mdates.DateConverter): datemax = pydt.date(2010, 1, 1) return munits.AxisInfo(- majloc=majloc, majfmt=majfmt, label="", default_limits=(datemin, datemax)+ majloc=majloc, majfmt=majfmt, label="", default_limits=(datemin, datemax),+ description="days since matplotlib epoch" ) (Though would probably need to version-gate mpl until they bump the minver of mpl) Currently I minimally add to the included jpl units EpochConverter to opt in to the date formatter. In looking into this I discovered that the jpl units Epoch class/converter are actually incorrect in a couple of ways, most notably an off by one error that has seemingly been around since the beginning. All of our examples pass in The image tests of the jpl units are all actually incorrect (at least where dates are shown). e.g.: matplotlib/lib/matplotlib/tests/test_axes.py Lines 853 to 865 in8166596
This I would expect to have a vspan for the day of the 20th, but the image shows a vspan for the day of the 21st: |
I have a possibly ridiculous question, but is this and the approach in#25662 mutually exclusive? I'm not totally following, but it seems like you're kinda trying to solve different but related problems with each of these approaches, and I'm wondering if different downstreams will need the validation in different places. Granted, I also really like your idea of registering formatters with convertors. |
PR Summary
This is an alternative implementation of#25662, validating in the opposite direction
Closes#24951
In#25662, the converter holds reference to what formatters are valid.
Here, The formatter holds reference to what converters are valid.
Another difference in implementation is that the validators raise rather than return
boolean.
With respect to the original report (#24951), with current main of nc-time-axis there is
no change in behavior (no warning, produces incorrect dates). With the change proposed
inSciTools/nc-time-axis#127, it errors on validation. Their provided formatters will
provide no validation, but also will not fail
(until they implement the validator method)
ping@spencerkclark for nc-time-axis's stake in this
It is pretty minimal and easy, but tests currently fail because of the jpl_units EpochConverter.
EpochConverter does not inherit from DateConverter, butis compatible (the inverse of current main of nc-time-axis).
Since we are pretty strict about not changing the jpl units, I'm looking for some other ideas...
A couple ideas:
converter.axis_info
, and automatically allow the type returned from that (for maj or min).DateFormatter
orConciseDateFormatter
, but that can't be teased out from axis_infoDateFormatter
s which does not rely onisinstance
try: assert converter.convert(datetime.date(...), ...) == ...; except: raise ...
assert
forpython -O
reasons, but same idea)PR Checklist
Linked Issue
Documentation and Tests
pytest
passes)Release Notes
.. versionadded::
directive in the docstring and documented indoc/users/next_whats_new/
.. versionchanged::
directive in the docstring and documented indoc/api/next_api_changes/
next_whats_new/README.rst
ornext_api_changes/README.rst