Skip to content

Plotting NaT with matplotlib results in error #9253

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
jvspl opened this issue Jan 14, 2015 · 2 comments
Closed

Plotting NaT with matplotlib results in error #9253

jvspl opened this issue Jan 14, 2015 · 2 comments
Labels
Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Visualization plotting

Comments

@jvspl
Copy link

jvspl commented Jan 14, 2015

Trying to plot the NaT (not a time) value of pandas gives an error, where plotting NaN does not.
Expected (and hereby requested) is that NaT is treated just like NaN - i.e., ignore it.

from pylab import *
import pandas as pd
plot(pd.NaT,5)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-60addc339c2f> in <module>()
----> 1 plot(pd.NaT,5)

C:\Anaconda\lib\site-packages\matplotlib\pyplot.pyc in plot(*args, **kwargs)
   3091         ax.hold(hold)
   3092     try:
-> 3093         ret = ax.plot(*args, **kwargs)
   3094         draw_if_interactive()
   3095     finally:

C:\Anaconda\lib\site-packages\matplotlib\axes\_axes.pyc in plot(self, *args, **kwargs)
   1372 
   1373         for line in self._get_lines(*args, **kwargs):
-> 1374             self.add_line(line)
   1375             lines.append(line)
   1376 

C:\Anaconda\lib\site-packages\matplotlib\axes\_base.pyc in add_line(self, line)
   1484             line.set_clip_path(self.patch)
   1485 
-> 1486         self._update_line_limits(line)
   1487         if not line.get_label():
   1488             line.set_label('_line%d' % len(self.lines))

C:\Anaconda\lib\site-packages\matplotlib\axes\_base.pyc in _update_line_limits(self, line)
   1495         Figures out the data limit of the given line, updating self.dataLim.
   1496         """
-> 1497         path = line.get_path()
   1498         if path.vertices.size == 0:
   1499             return

C:\Anaconda\lib\site-packages\matplotlib\lines.pyc in get_path(self)
    863         """
    864         if self._invalidy or self._invalidx:
--> 865             self.recache()
    866         return self._path
    867 

C:\Anaconda\lib\site-packages\matplotlib\lines.pyc in recache(self, always)
    564                 x = ma.asarray(xconv, np.float_)
    565             else:
--> 566                 x = np.asarray(xconv, np.float_)
    567             x = x.ravel()
    568         else:

C:\Anaconda\lib\site-packages\numpy\core\numeric.pyc in asarray(a, dtype, order)
    460 
    461     """
--> 462     return array(a, dtype, copy=False, order=order)
    463 
    464 def asanyarray(a, dtype=None, order=None):

TypeError: float() argument must be a string or a number

Calling plot with a np.nan results in the point being silently ignored, which I believe should also happen for pd.NaT.

plot(np.nan,5)

This seems related to #8914, but this example gives an error, where the example of #8914 only results in wrong axes.

This issue was posted in the matplotlib issue tracker as matplotlib/matplotlib#4000, but they believe it can only be fixed in pandas.

@TomAugspurger
Copy link
Contributor

We won't be able to handle your exact case, which is calling the matplotlib plot function. As was pointed out in their issue, there's no way for them to know about our NaT. NaT is datetime like, so float(NaT) being NaN wouldn't work.

We could handle Series.plot and DataFrame.plot, with NaTs, e.g. df.plot(x='A', y'B') where x is a Datetime column w/ some NaTs, by applying the same rules we use for missing values (NaNs).

@jreback jreback added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Visualization plotting labels Jan 16, 2015
shoyer added a commit to shoyer/pandas that referenced this issue Feb 4, 2015
Some progress on pandas-dev#9253.

Note: this did not work. I'm not quite sure why.
@mroeschke
Copy link
Member

I believe #8914 more appropriately captures that NaT should be supported in pandas plotting functions as Tom mentions we cannot do anything on the matplotlib side.

Going to close this in favor of that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Visualization plotting
Projects
None yet
Development

No branches or pull requests

4 participants