pandas 0.21.0 Timestamp compatibility issue with matplotlib

后端 未结 2 1205

I just updated pandas from 0.17.1 to 0.21.0 to take advantage of some new functionalities, and ran into compatibility issue with matplotlib (which I also updated to latest 2

相关标签:
2条回答
  • 2020-11-28 12:38

    There is an issue with pandas datetimes and matplotlib coming from the recent release of pandas 0.21, which does not register its converters any more at import. Once you use those converters once (within pandas) they'll be registered and automatically used by matplotlib as well.

    A workaround would be to register them manually,

    import pandas.plotting._converter as pandacnv
    pandacnv.register()
    

    In any case the issue is well known at both pandas and matplotlib side, so there will be some kind of fix for the next releases. Pandas is thinking about readding the register in an upcomming release. So this issue may be there only temporarily. An option is also to revert to pandas 0.20.x where this should not occur.

    Update: this is no longer an issue with current versions of matplotlib (2.2.2)/pandas(0.23.1), and likely many that have been released since roughly December 2017, when this was fixed.

    Update 2: As of pandas 0.24 or higher the recommended way to register the converters is

    from pandas.plotting import register_matplotlib_converters
    register_matplotlib_converters()
    

    or if pandas is already imported as pd,

    pd.plotting.register_matplotlib_converters()
    
    0 讨论(0)
  • 2020-11-28 12:47

    After opening an issue on pandas github, I learned that this was indeed a known issue between pandas and matplotlib regarding auto-registration of unit converter. In fact it was listed on what's new page which I had failed to see before, along with the proper way to register the converters:

    from pandas.tseries import converter
    converter.register() 
    

    This is also done the first time a member plot method is called on a Series or DataFrame which explains what I observed above.

    It appears to have been done with the intention that matplotlib is supposed to implement some basic support for pandas datetime, but indeed a deprecation warning of some sort could be useful for such a break. However until matplotlib actually implements such support (or some sort of lazy registration mechanism), practically I'm always putting those two lines at the pandas import. So I'm not sure why pandas would want to disable the automatic registration on import before things are ready on the matplotlib side.

    0 讨论(0)
提交回复
热议问题