Skip to content

TypeError when plotting TimeSeries with NaN data, maybe a BUG? #5310

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
meelmaar opened this issue Oct 24, 2013 · 7 comments
Closed

TypeError when plotting TimeSeries with NaN data, maybe a BUG? #5310

meelmaar opened this issue Oct 24, 2013 · 7 comments

Comments

@meelmaar
Copy link

I get a TypeError message when I try to plot a pandas TimeSeries object that contains NaN's. The strange thing is, this does not happen when I have 'inf' values, or when a normal series is used with integer index values.

Now, all of a sudden (no recent updates I can think of) all my plotting scripts get the TypeError, but before they worked fine. I can do dropna() before, but then missing data is interpolated in the plot, which I do not want; the data is after all missing... If I do fillna(np.inf), things are plotting as I like.

As discussed below, I am not sure this is a bug in pandas 0.12 / matplotlib 1.3 but something can be wrong with my installation. I just don't know what that can be and a fresh install is not an option now. I hope someone can test the code and provide ideas on how I can solve the problem?

The following code causes problems:

import numpy as np
import pandas as pd
s = pd.Series(np.arange(10.))
rng = pd.date_range('2012-12-12', periods=10, freq='H')
ts = pd.Series(np.arange(10.), index=rng)

ts.plot()  # Works fine
s.plot()   # Works fine
# now, if I replace one value with an NaN
ts[2] = np.NaN

ts.plot() # gives the following error:

Traceback (most recent call last):

  File "<ipython-input-6-132f3667ee95>", line 1, in <module>
    ts.plot()

  File "C:\Python27\lib\site-packages\pandas\tools\plotting.py", line 1730, in plot_series
    plot_obj.generate()

  File "C:\Python27\lib\site-packages\pandas\tools\plotting.py", line 856, in generate
    self._make_plot()

  File "C:\Python27\lib\site-packages\pandas\tools\plotting.py", line 1240, in _make_plot
    self._make_ts_plot(data, **self.kwds)

  File "C:\Python27\lib\site-packages\pandas\tools\plotting.py", line 1311, in _make_ts_plot
    _plot(data, 0, ax, label, self.style, **kwds)

  File "C:\Python27\lib\site-packages\pandas\tools\plotting.py", line 1295, in _plot
    style=style, **kwds)

  File "C:\Python27\lib\site-packages\pandas\tseries\plotting.py", line 77, in tsplot
    lines = plotf(ax, *args, **kwargs)

  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4137, in plot
    for line in self._get_lines(*args, **kwargs):

  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):

  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 295, in _plot_args
    x, y = self._xy_from_xy(x, y)

  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 214, in _xy_from_xy
    by = self.axes.yaxis.update_units(y)

  File "C:\Python27\lib\site-packages\matplotlib\axis.py", line 1336, in update_units
    converter = munits.registry.get_converter(data)

  File "C:\Python27\lib\site-packages\matplotlib\units.py", line 137, in get_converter
    xravel = x.ravel()

  File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3969, in ravel
    r._mask = ndarray.ravel(self._mask).reshape(r.shape)

  File "C:\Python27\lib\site-packages\pandas\core\series.py", line 981, in reshape
    return ndarray.reshape(self, newshape, order)

TypeError: an integer is required

# If I do:
 ts[2] = np.inf   # Then ts.plot() works...

I am using pandas 0.12 with matplotlib 1.3 on python x,y 2.7.5.1 on Windows 7.
Upgrading to matplotlib 1.3.1 or downgrading to pandas 0.11 did not solve the problem.

I have tried the same code in an WinPython-64bit -2.7.4.1.amd64 that I have installed and then the code works fine. Dependencies are then:
pandas 0.11 and matplotlib 1.2.1. Maybe this indicates something is wrong with my python x,y installation?

Happy to provide more details. Looking forward to your help!

Maarten

@jorisvandenbossche
Copy link
Member

I don't have the problem on Windows 7, pandas 0.12 and matplotlib 1.3.0 (installed through Anaconda). Strange.

@jtratner
Copy link
Contributor

If your codr previously worked with pandas 0.11 and downgrading to 0.11 did
not fix it, that suggests that some other change caused the problem. Though
it does seem strange that that would be the case.

@goyodiaz
Copy link
Contributor

I can't replicate the issue, see http://nbviewer.ipython.org/7135636.
That said, as similar issue led me to file bug #4554. You can try building a development version (there are no prebuilt binaries AFAIK) or patching series.reshape as in 89a65d2.

@meelmaar
Copy link
Author

Thanks all for testing on your systems.
I still find it strange that I have the error and you don't but tried the patching of series.reshape as in 89a65d2 (thanks @goyodiaz). This indeed solved my problems and the problem of #4544. Looking forward to pandas 0.13, but a the moment a bit reluctant on trying the development version; maybe do a fresh install first;)

Again many thanks. I think we can close the issue!

@jreback jreback closed this as completed Oct 24, 2013
@jtratner
Copy link
Contributor

We'll have a release candidate soon - if you're willing to install it and
test, we always appreciate feedback and bug reports. (Though we make sure
that master always passes our test suite...so it ought to be pretty
stable [no guarantees obv])

@meelmaar
Copy link
Author

You are right. I should give it a try, even though not a crack at all; maybe it helps the other non-cracks ;). I'll give it a go and let you know!

@jtratner
Copy link
Contributor

Great! :) Should have a release candidate soon (though it's going to be
nearly the same as current master at this point)

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

No branches or pull requests

5 participants