Skip to content

BUG: strange timeseries plot behavior #6608

Closed
@rosnfeld

Description

@rosnfeld

After some discussion below, here's a simple repro case:

s1 = pd.Series([1, 2, 3], index=[datetime.datetime(1995, 12, 31), datetime.datetime(2000, 12, 31), datetime.datetime(2005, 12, 31)])
s2 = pd.Series([1, 2, 3], index=[datetime.datetime(1997, 12, 31), datetime.datetime(2003, 12, 31), datetime.datetime(2008, 12, 31)])

# plot first series, then add the second series to those axes, then try adding the first series again
ax = s1.plot()
s2.plot(ax=ax)
s1.plot(ax=ax)

causes

Traceback (most recent call last):
  File "simple_repro.py", line 10, in <module>
    s1.plot(ax=ax)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 2116, in plot_series
    plot_obj.generate()
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 920, in generate
    self._make_plot()
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 1482, in _make_plot
    self._make_ts_plot(data)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 1577, in _make_ts_plot
    _plot(data, 0, ax, label, self.style, **kwds)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 1553, in _plot
    style=style, **kwds)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tseries/plotting.py", line 82, in tsplot
    left, right = _get_xlim(ax.get_lines())
  File "/home/andrew/git/pandas-rosnfeld/pandas/tseries/plotting.py", line 226, in _get_xlim
    left = min(x[0].ordinal, left)
AttributeError: 'datetime.datetime' object has no attribute 'ordinal'

-- ORIGINAL MESSAGE --

Here's a small dataset:

date,region,value
1996-12-31,BRA,4.5
2003-12-31,BRA,3.7
2007-12-31,BRA,2.2
1995-12-31,COL,6.3
2000-12-31,COL,4.9
2005-12-31,COL,5.1
2010-12-31,COL,3.4
1997-12-31,PAN,6.3
2003-12-31,PAN,5.1
2008-12-31,PAN,3.9
1990-12-31,VEN,6.7
1991-12-31,VEN,5.4
1992-12-31,VEN,4.5
1993-12-31,VEN,4
1994-12-31,VEN,3.9
1995-12-31,VEN,4.1
1996-12-31,VEN,4.4
1997-12-31,VEN,4.5
1998-12-31,VEN,4.6
1999-12-31,VEN,4.1
2000-12-31,VEN,3.9
2007-12-31,VEN,3.7

If I read this in using

data = pd.read_csv('./data.csv', parse_dates='date', index_col='date')

and then try and plot it using

data.groupby('region').value.plot(legend=True)

I get more or less what I expect (perhaps the xlim doesn't go up to 2010-12-31, but otherwise fine).

If I delete out the BRA rows and try this again, I get:

Traceback (most recent call last):
  File "repro.py", line 6, in <module>
    data.groupby('region').value.plot()
  File "/home/andrew/git/pandas-rosnfeld/pandas/core/groupby.py", line 342, in wrapper
    return self.apply(curried)
  File "/home/andrew/git/pandas-rosnfeld/pandas/core/groupby.py", line 428, in apply
    return self._python_apply_general(f)
  File "/home/andrew/git/pandas-rosnfeld/pandas/core/groupby.py", line 432, in _python_apply_general
    self.axis)
  File "/home/andrew/git/pandas-rosnfeld/pandas/core/groupby.py", line 958, in apply
    res = f(group)
  File "/home/andrew/git/pandas-rosnfeld/pandas/core/groupby.py", line 426, in f
    return func(g, *args, **kwargs)
  File "/home/andrew/git/pandas-rosnfeld/pandas/core/groupby.py", line 333, in curried
    return f(x, *args, **kwargs)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 1921, in plot_series
    plot_obj.generate()
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 912, in generate
    self._make_plot()
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 1379, in _make_plot
    self._make_ts_plot(data, **self.kwds)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 1450, in _make_ts_plot
    _plot(data, 0, ax, label, self.style, **kwds)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tools/plotting.py", line 1434, in _plot
    style=style, **kwds)
  File "/home/andrew/git/pandas-rosnfeld/pandas/tseries/plotting.py", line 82, in tsplot
    left, right = _get_xlim(ax.get_lines())
  File "/home/andrew/git/pandas-rosnfeld/pandas/tseries/plotting.py", line 226, in _get_xlim
    left = min(x[0].ordinal, left)
AttributeError: 'datetime.datetime' object has no attribute 'ordinal'

If I delete out both BRA and VEN rows, then there is no exception raised but I only see one series plotted and the x-axis is not formatted as a date.

One could also approach this whole exercise via something like

data = pd.read_csv('./data.csv', parse_dates='date')
data.pivot('date', 'region', 'value').plot()

but this works even worse, I just get a truncated VEN series and nothing else.

This is with current master pandas (but also happens in 0.13.1) and matplotlib 1.3.1.

Are there known issues with plotting sparse-yet-overlapping timeseries?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeTestingpandas testing functions or related to the test suiteVisualizationplotting

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions