-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Recognize pandas Timestamp objects for DateConverter? #10533
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
Is there a way to do that without importing pandas? I don't think we want to unconditionally try to import pandas (for the same reasons pandas does not want to unconditionally try to import matplotlib). Could both mpl and pandas do if other_one in sys.modules:
pd.plotting.register_matplotlib_converters() I think that would let both be lazy and not import the other and get around import-order issues. |
Hmm, yes, I didn't think of that problem ..
Yes, if we both tried to register it lazily, that sounds like a possible solution. |
Just spitballin' here, but have we considered maybe utilizing pluggy for
things like this, and let other packages register conerters that way? We
could probably also use pluggy for more things, too, like custom colormaps
and custom event handlers.
…On Mon, Feb 19, 2018 at 12:41 PM, Joris Van den Bossche < ***@***.***> wrote:
Hmm, yes, I didn't think of that problem ..
I think that would let both be lazy and not import the other and get
around import-order issues.
Yes, if we both tried to register it lazily, that sounds like a possible
solution.
(we only need to register something different as pd.plotting.register_
matplotlib_converters(), because that one is registering our *own, custom*
converters, while in this case we want to register matplotlib's
DateConverter)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#10533 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARy-JpUshpx8UeM-wmTSHkMKNgdukfdks5tWbJcgaJpZM4SK5Dx>
.
|
Pushing this to 3 as I am not sure there is a quick fix. |
Your idea above would not be accepted if I did a PR?
@WeatherGod it's not really the problem that it is difficult to register converters (pandas does now register converters by adding to the |
My suggested above would be, |
Another problem is I am making an overview of all reported issues and checking if they run now on matplotlib 2.2 (without pandas' converters): http://nbviewer.jupyter.org/gist/jorisvandenbossche/926234900d5f54ef4e1016d77098ff46 I think we could also solve this in a more general way that does not depend on checking the presence of pandas. But doing the lazy check if pandas is available as discussed above would also work. Not sure which approach would be preferential. |
Or, another implicit method without importing pandas is to also try to infer the converter for the matplotlib/lib/matplotlib/cbook/__init__.py Line 2341 in 180639b
that is used to get the x and y data in case of |
What's nice about the pluggy approach (and I agree that such an approach
should go into v3.x) is that it is a general solution for the import order
problem. You are essentially registering everything to a third-party, if
you will, and so if matplotlib is already imported, then great! If it isn't
imported yet, then matplotlib will still get that information when it does
get imported eventually.
It is something to think about now as a way to think about the ways people
customize matplotlib. We currently don't have a nice way to register
colormaps, and the event handling system could use an overhaul anyway. If
we provide a similar way to register things throughout matplotlib, that
would be a plus for the ecosystem.
…On Mon, Feb 19, 2018 at 7:11 PM, Joris Van den Bossche < ***@***.***> wrote:
cc @TomAugspurger <https://github.com/tomaugspurger>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#10533 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARy-PfJjr3cQLc9aCHd1hK0Yc53Cy3cks5tWg2bgaJpZM4SK5Dx>
.
|
fwiw I think setuptools entry points may (possibly) be a simpler solution for plugin registration. |
Maybe someone can open a separate issue for dicussing plugin registration? I would like to keep this one focused on matplotlib actually being able to handle pandas objects properly. |
@tacaswell do you have any preference in what way to solve this?
My personal preference would be 2, but I can do a PR for whathever matplotlib prefers. |
I have a preference for 1 because it is simpler and (I hope) non-controversial and should work universally on plotting method (so far as the unit handling works universally). I am concerned 2 would rapidly escalate in complexity (but I would be happy to be wrong). |
I mildly disagree that Matplotlib should be responsible for registering converters provided by downstream libraries (following arguments @efiring made when this first came up). I'm fine w/ native types, numpy types, and datetime types being registered, primarily because we maintain those converters. But if we register downstream libraries on our end, where does it stop? Should Matplotlib register |
@jklymak I hear you and @efiring on this, but I am still 👍 on giving pandas special treatment (we already do ;) ). Given @WeatherGod 's comments above we should investigate doing a proper plugin system to solve the lazy-import-order issues to handle the general case. |
OK, I looked at this quickly. |
I think you can still check if pandas is already imported or not. And if not, but is importable, register the types. However, I am personally more in favor of the other approach (approach 2):
I personally think that option 2 is cleaner, and can be done in a generic way without being to explicitly tied to pandas (checking for the dtype of arrays is rather generic). |
I think we should move in the direction of (2); as has been pointed out above, we are already part of the way along that path, and I don't think that going the rest of the way need be very difficult. It can be done incrementally, if necessary. (#10631 is actually one such incremental step.) It is a clean solution--so long as pandas doesn't change their data structures too much. |
Units now work via a registry, whereby you need to register data types and then they get sent to converters. I don't see how that'll work cleanly with implicit data types without some surgery in |
Just a little surgery. Something like this: inside
put
That's the duck-typing approach. Maybe not squeaky-clean, but I think it would work. Easy to say before testing, of course. |
We have #10638 in which I think addresses this problem, but keeping this open and moving to 2.2.1 just in case. |
@jorisvandenbossche Can this be closed now? Or is there more to do? |
I'll assume this is closed |
I am testing matplotlib 2.2rc from a point of view of "what if pandas no longer registers its unit converters", and one of the issues raised when we temporarily did that for the 0.21.0 release was the following (pandas-dev/pandas#18283):
Code for reproduction
Actual outcome
The above raises
TypeError: Cannot compare type 'Timestamp' with type 'float'
, because Timestamps are not recognized to use theDateConverter
in the units framework.Would it be possible to consider adding pandas Timestamps to the unit registry? It are subclasses of
datetime.datetime
, so code-wise it should not be hard to add support.Matplotlib version
The text was updated successfully, but these errors were encountered: