作者:少少_LV | 来源:互联网 | 2023-05-21 19:00
我有一个Pandas TimeSeries,其中日期有一个特定的时区.
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)
当我想用它绘图时events.plot()
,我得到以下问题:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
----> 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'
这必须是时区的问题.当我替换掉时区时dates = pd.date_range('2014-11-01T00:00:00', periods=100, freq='D')
,events.plot()
生成一个没有问题的漂亮图表.
我可以通过这样做轻松解决它dates = dates.tz_convert("UTC")
,但有更自然的方法吗?
谢谢