Skip to content

plot_date() does not work with x values of type pandas.Timestamp (pandas version 0.15.0)? #3727

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

Closed
pybokeh opened this issue Oct 26, 2014 · 20 comments
Labels

Comments

@pybokeh
Copy link

pybokeh commented Oct 26, 2014

Please see my ipython notebook gist:
http://nbviewer.ipython.org/gist/pybokeh/f2aab3aafdc287d02bb8

Basically, if I try to plot_date() using x values of type datetime, I get weird errors. But if I convert the x values to type datetime.date, then it plots ok.

EDIT: the only difference between In[3] and In[4] is I used list comprehension in the plot_date() method in In[4] to convert the x values to type datetime.date.

@pybokeh
Copy link
Author

pybokeh commented Oct 26, 2014

Here is my original script that I made this past January which worked fine back then. So looks like now it doesn't work with newer versions of MATPLOTLIB or newer version of pandas.

http://nbviewer.ipython.org/gist/pybokeh/416b6fb2910475ef8e68

@efiring
Copy link
Member

efiring commented Oct 27, 2014

Would you strip this down to a SSCCE, please? Preferably a script with minimal dependencies, e.g., no pandas.

@tacaswell
Copy link
Member

I suspect that this is more datetime64 issues.

@pybokeh
Copy link
Author

pybokeh commented Oct 27, 2014

OK so my title is misleading, it should say plot_date() doesn't work with x values of type pandas.Timestamp as this simplified example shows:

http://nbviewer.ipython.org/gist/pybokeh/92d28fa729bd38be8b92

It is throwing exception:
FormatterWarning: Exception in image/png formatter: Python int too large to convert to C long

@pybokeh pybokeh changed the title plot_date() does not work with x values of type datetime? plot_date() does not work with x values of type pandas.Timestamp? Oct 27, 2014
@tacaswell
Copy link
Member

That warning is coming out of IPython code, not out of mpl code. Can you reproduce without ipython and without pandas?

attn @takluyver @jreback can you point someone for your projects at this?

@tacaswell tacaswell added this to the unassigned milestone Oct 27, 2014
@tacaswell tacaswell added the status: needs clarification Issues that need more information to resolve. label Oct 27, 2014
@pybokeh
Copy link
Author

pybokeh commented Oct 27, 2014

I can not re-produce without pandas. The problem appears to be with pandas Timeseries object which is why I changed the title. Whether or not plot_date() should work with pandas.Timeseries, that I can not say. I believe back in January, my original bitcoin chart script did work, but I would have to duplicate the same environment that I had back then to confirm what the exact data that I had passed in.

This works fine:
from datetime import datetime
import matplotlib.pyplot as plt

d1 = datetime(2001,5,1)
d2 = datetime(2001,5,2)
d3 = datetime(2001,5,3)

dates = []
dates.append(d1)
dates.append(d2)
dates.append(d3)

values = [1,2,3]

plt.plot_date(dates, values, 'r')
plt.show()

type(dates[0]) returns:
datetime.datetime

@WeatherGod
Copy link
Member

The change might be because of some fixes to applying units conversion (an
internal thing), which might have broken some things. If I understand
pandas correctly though, dates from a pandas object is just simply numpy's
datetime64[ns] object? I could be wrong, though.

On Mon, Oct 27, 2014 at 11:42 AM, pybokeh notifications@github.com wrote:

I can not re-produce without pandas. The problem appears to be with pandas
Timeseries object which is why I changed the title. Whether or not
plot_date() should work with pandas.Timeseries, that I can not say. I
believe back in January, my original bitcoin chart script did work, but I
would have to duplicate the same environment that I had back then to
confirm what the exact data that I had passed in.

This works fine:
from datetime import datetime
import matplotlib.pyplot as plt

d1 = datetime(2001,5,1)
d2 = datetime(2001,5,2)
d3 = datetime(2001,5,3)

dates = []
dates.append(d1)
dates.append(d2)
dates.append(d3)

values = [1,2,3]

plt.plot_date(dates, values, 'r')
plt.show()

type(dates[0]) returns:
datetime.datetime


Reply to this email directly or view it on GitHub
#3727 (comment)
.

@jenshnielsen
Copy link
Member

Is it possible that the issue is due to a upgrade of Pandas version? Pandas have changed some of their objects to no longer subclass numpy ndarray which has previously broken plotting of pandas object. See http://matplotlib.org/faq/usage_faq.html?highlight=pandas#types-of-inputs-to-plotting-functions

@jreback
Copy link

jreback commented Oct 27, 2014

pd.Timestamp is a direct sub-class of datetime.datetime

@takluyver
Copy link
Contributor

@tacaswell - the warning comes from IPython, but that's because IPython catches an error in a formatter and turns it into a warning, I believe.

@pybokeh
Copy link
Author

pybokeh commented Oct 27, 2014

OK, at work I created an environment with pandas 0.14.1 and my bitcoin chart script works. Uninstalled pandas 0.14.1 and then installed pandas 0.15.0. Now, it does not work.

EDIT:
Sorry I should have revealed what my environment was for my issue that I opened.

Python 3.4.2,
pandas 0.15.0
matplotlib 1.4.2

So looks like the issue has to do with something that has apparently changed with pandas 0.15.0.

@pybokeh pybokeh changed the title plot_date() does not work with x values of type pandas.Timestamp? plot_date() does not work with x values of type pandas.Timestamp (pandas version 0.15.0)? Oct 27, 2014
@jorisvandenbossche
Copy link

This is a 'regression' in pandas 0.15 (due to the refactor of Index), see pandas-dev/pandas#8614.
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.

Running units.registry[np.datetime64] = pd.tseries.converter.DatetimeConverter() should solve it for now. Can you try that?

@jorisvandenbossche
Copy link

pandas-dev/pandas#8693 should solve this

@WeatherGod
Copy link
Member

Actually, wouldn't it make sense to get this into matplotlib? Supporting
datetime64 was a feature that got lost with John's passing (he understood
the units system the best and we talked about it at SciPy2012).

On Wed, Nov 5, 2014 at 9:44 AM, Joris Van den Bossche <
notifications@github.com> wrote:

pandas-dev/pandas#8693 pandas-dev/pandas#8693 should
solve this


Reply to this email directly or view it on GitHub
#3727 (comment)
.

@jorisvandenbossche
Copy link

yes certainly (the issue for this: #1097)
and that is what we also discussed briefly in pandas-dev/pandas#8614 (comment), but there is someone who just needs to do this (and depending on what we exactly want (basic datetime64 support, or refactoring of date support using datetime64))

The merged PR (pandas-dev/pandas#8693) just solves this specific issue regarding plotting a pandas DatetimeIndex which worked previously but broke in pandas 0.15.0

@petehuang
Copy link
Contributor

All gist links are broken. Is there another example that we can use to confirm the fix provided?

@jorisvandenbossche
Copy link

This issue can actually just be closed I think. The actual issue reported here is closed by a PR in pandas in version 0.15.1(so only pandas 0.15.0 has this problem), and the larger issue (better datetime support in matplotlib) is covered by #1097

@pybokeh
Copy link
Author

pybokeh commented Jan 3, 2017

I re-ran my old examples and confirmed that with latest version of mpl and pandas, all works ok. Closing this issue.

@pybokeh pybokeh closed this as completed Jan 3, 2017
@liubenyuan
Copy link

It seems like this problem happens in pandas 0.21.0.

code to reproduce this error

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import dates

t = np.arange(10)
ts = pd.datetime(2017, 11, 16) + pd.to_timedelta(t, 's')
df = pd.DataFrame(t, index=ts)

fig, ax = plt.subplots()
ax.plot(df)

# format time axis
hfmt = dates.DateFormatter('%m/%d %H:%M')
ax.xaxis.set_major_formatter(hfmt)
fig.autofmt_xdate()
plt.show()

The trace

Traceback (most recent call last):

  File "D:\Anaconda3\lib\site-packages\IPython\core\formatters.py", line 332, in __call__
    return printer(obj)

  File "D:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 237, in <lambda>
    png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))

  File "D:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py", line 121, in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File "D:\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2208, in print_figure
    **kwargs)

  File "D:\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 507, in print_png
    FigureCanvasAgg.draw(self)

  File "D:\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py", line 430, in draw
    self.figure.draw(self.renderer)

  File "D:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "D:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1295, in draw
    renderer, self, artists, self.suppressComposite)

  File "D:\Anaconda3\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)

  File "D:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "D:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 2399, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)

  File "D:\Anaconda3\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)

  File "D:\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "D:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1133, in draw
    ticks_to_draw = self._update_ticks(renderer)

  File "D:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 974, in _update_ticks
    tick_tups = list(self.iter_ticks())

  File "D:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 921, in iter_ticks
    for i, val in enumerate(majorLocs)]

  File "D:\Anaconda3\lib\site-packages\matplotlib\axis.py", line 921, in <listcomp>
    for i, val in enumerate(majorLocs)]

  File "D:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 538, in __call__
    dt = num2date(x, self.tz)

  File "D:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 441, in num2date
    return _from_ordinalf(x, tz)

  File "D:\Anaconda3\lib\site-packages\matplotlib\dates.py", line 256, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)

OverflowError: Python int too large to convert to C long

<matplotlib.figure.Figure at 0xebecc88>

@jorisvandenbossche
Copy link

jorisvandenbossche commented Nov 16, 2017

See #9610 (short story is that pandas no longer automatically registers its unit converters starting from 0.21.0), but we will fix this in upcoming 0.21.1 to at least first give deprecation warning (pandas-dev/pandas#18301)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests