Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when plotting a pandas timeseries, if dates have timezones

I have a Pandas TimeSeries, where the dates have a specific timezone.

import pandas as pd
dates = pd.date_range('2014-11-01T00:00:00+0100', periods=100, freq='D')
events = pd.Series(np.random.randint(5, 500, 100), index=dates)

When I want to plot it with events.plot(), I get the following issue:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-2f72296bdf26> in <module>()
----> 1 events.plot()

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   2509                  yerr=yerr, xerr=xerr,
   2510                  label=label, secondary_y=secondary_y,
-> 2511                  **kwds)
   2512 
   2513 

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   2315         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   2316 
-> 2317     plot_obj.generate()
   2318     plot_obj.draw()
   2319     return plot_obj.result

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in generate(self)
    921         self._compute_plot_data()
    922         self._setup_subplots()
--> 923         self._make_plot()
    924         self._add_table()
    925         self._make_legend()

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in _make_plot(self)
   1586         self._initialize_prior(len(self.data))
   1587 
-> 1588         if self._is_ts_plot():
   1589             data = self._maybe_convert_index(self.data)
   1590             x = data.index      # dummy, not used

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in _is_ts_plot(self)
   1581     def _is_ts_plot(self):
   1582         # this is slightly deceptive
-> 1583         return not self.x_compat and self.use_index and self._use_dynamic_x()
   1584 
   1585     def _make_plot(self):

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in _use_dynamic_x(self)
   1577                 return False
   1578 
-> 1579         return (freq is not None) and self._is_dynamic_freq(freq)
   1580 
   1581     def _is_ts_plot(self):

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in _is_dynamic_freq(self, freq)
   1551             freq = frequencies.get_base_alias(freq)
   1552         freq = frequencies.get_period_alias(freq)
-> 1553         return freq is not None and self._no_base(freq)
   1554 
   1555     def _no_base(self, freq):

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tools/plotting.py in _no_base(self, freq)
   1561             x = self.data.index
   1562             if (base <= frequencies.FreqGroup.FR_DAY):
-> 1563                 return x[:1].is_normalized
   1564 
   1565             return Period(x[0], freq).to_timestamp(tz=x.tz) == x[0]

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/lib.cpython-34m.so in pandas.lib.cache_readonly.__get__ (pandas/lib.c:40766)()

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tseries/index.py in is_normalized(self)
   1481         Returns True if all of the dates are at midnight ("no time")
   1482         """
-> 1483         return tslib.dates_normalized(self.asi8, self.tz)
   1484 
   1485     @cache_readonly

/home/kilik/.virtualenvs/data_analysis3/local/lib/python3.4/site-packages/pandas/tslib.cpython-34m.so in pandas.tslib.dates_normalized (pandas/tslib.c:58465)()

AttributeError: '_FixedOffset' object has no attribute '_transition_info'

This must be an issue with the timezone. When I replace take off the timezones with dates = pd.date_range('2014-11-01T00:00:00', periods=100, freq='D'), events.plot() generates a beautiful graph with no issue.

I can easily fix it by doing dates = dates.tz_convert("UTC"), but is there a more natural way to do it?

Thanks

like image 870
Romain Endelin Avatar asked Feb 16 '15 10:02

Romain Endelin


1 Answers

The easiest and cleanest way to solve this (IMO) is to use the date accessor:

dates = pd.date_range('2014-11-01T00:00:00+0100', periods=100, freq='D')
events = pd.Series(np.random.randint(5, 500, 100), index=dates.date)
events.plot(rot=90)

The result:

enter image description here

like image 58
Yuca Avatar answered Nov 14 '22 22:11

Yuca