-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Try to get a units converter for a type before falling back to built-in behavior #20502
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
Comments
My understanding is that the units registry works on the data type of the first element of the iterable, not the iterable. i.e. |
@jklymak That is the current behavior for iterables, yes. For objects not caught by the handful of built-in cases, the converter is used on the object. For example:
During a call to Perhaps this is an abuse of the units system and so it may not make sense as part of the units module, but it seems reasonable to be able to, given an arbitrary object, register how it would like to be interpreted for plotting. |
I think you may want to create a custom dtype. https://stackoverflow.com/questions/2350072/custom-data-types-in-numpy-arrays |
More to your immediate concern, matplotlib can handle nanoseconds from an epoch already using datetime64. Right now our default epoch is 1970-01-01, and nanoseconds in datetime64 have +/-292y of usable range. So unless you plan to do nanoseconds for more than 584 years, something like |
I agree that casting to |
It looks the behavior of looking at matplotlib/lib/matplotlib/units.py Lines 193 to 197 in 4f2f4de
|
Perhaps moving the transversal of the mro to the front would make sense? But I am still unsure of whether or not the units module is intended to handle coercing arbitrary objects into arrays-likes for plotting or just coercing unrecognized scalars to numbers. |
When given an object to plot, should matplotlib attempt to use a registered
units.ConversionInterface
for that object's type (and mro) before falling back to other built-in defaults?This question/suggestion prompted by the following example:
Say one has a class
subclassing
np.ndarray
with the convention thatDateTimeNano
stores nano-seconds since epoch. When plotting aDateTimeNano
, one may want to make use of the built in utilities for date tick location/formatting. If one writes aunits.ConversionInterface
likeregisters it with
and then calls
only
DateTimeNanoConverter.default_units
andDateTimeNanoConverter.axisinfo
are used.DateTimeNanoConverter.convert
is skipped entirely asunits._is_natively_supported
recognizes that it is an iterable and so a converter for the typeDateTimeNano
is never searched for (it also appears that this would be short circuited inunits.Registry.get_converter
).If the step of searching for a converter by iterating over the type's mro were moved up in the order of trying to get values out of an object, this problem would not occur. It also seems like an overall preferred behavior as searching for a way to get an array of values out (using a converter or built-in code paths) should always start with the most specific implementation and work out.
This does not currently happen if types happen to have a particular inheritance structure or set of attributes.
The text was updated successfully, but these errors were encountered: