Skip to content Skip to sidebar Skip to footer

Why Do I Get "python Int Too Large To Convert To C Long" Errors When I Use Matplotlib's Dateformatter To Format Dates On The X Axis?

Following this answer's use of DateFormatter, I tried to plot a time series and label its x axis with years using pandas 0.15.0 and matplotlib 1.4.2: import datetime as dt import m

Solution 1:

This is a 'regression' in pandas 0.15 (due to the refactor of Index), see https://github.com/matplotlib/matplotlib/issues/3727 and https://github.com/pydata/pandas/issues/8614, but is fixed in 0.15.1.


Short story: matplotlib now sees the pandas index as an array of datetime64[ns] values (which are actually very large int64s), instead of an array of Timestamps (which are subclass of datetime.datetime, and can be handled by matplotlib) in previous versions of pandas. So the underlying reason is that matplotlib does not handle datetime64 as date values but as ints.

For pandas 0.15.0 (but better upgrade to a newer version), there are two possible workarounds:

  • Register the datetime64 type, so it will also be handled as a date by matplotlib:

    units.registry[np.datetime64] = pd.tseries.converter.DatetimeConverter()
    
  • Or convert the DatetimeIndex (with datetime64 values) to an array of datetime.datetime values with the to_pydatetime method, and plot this:

    ax1.plot(data.index.to_pydatetime(), data.GS10)
    

related question: Plotting datetimeindex on x-axis with matplotlib creates wrong ticks in pandas 0.15 in contrast to 0.14

Post a Comment for "Why Do I Get "python Int Too Large To Convert To C Long" Errors When I Use Matplotlib's Dateformatter To Format Dates On The X Axis?"