Skip to content

Commit 872ccfa

Browse files
author
Chang She
committed
ENH: small refactor of tseries.plotting and adding specialized PeriodIndex/DatetimeIndex plotting back in
1 parent 1fbde53 commit 872ccfa

File tree

3 files changed

+330
-619
lines changed

3 files changed

+330
-619
lines changed

pandas/tools/plotting.py

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ class LinePlot(MPLPlot):
309309

310310
def __init__(self, data, **kwargs):
311311
MPLPlot.__init__(self, data, **kwargs)
312+
self.has_ts_index = False
313+
from pandas.tseries.index import DatetimeIndex
314+
from pandas.tseries.period import PeriodIndex
315+
if isinstance(data.index, (DatetimeIndex, PeriodIndex)):
316+
self.has_ts_index = True
312317

313318
def _get_plot_function(self):
314319
if self.logy:
@@ -324,22 +329,71 @@ def _get_plot_function(self):
324329

325330
def _make_plot(self):
326331
# this is slightly deceptive
327-
x = self._get_xticks()
332+
if self.use_index and self.has_ts_index:
333+
data = self._maybe_convert_index(self.data)
334+
self._make_ts_plot(data)
335+
else:
336+
x = self._get_xticks()
337+
338+
plotf = self._get_plot_function()
339+
340+
for i, (label, y) in enumerate(self._iter_data()):
341+
if self.subplots:
342+
ax = self.axes[i]
343+
style = 'k'
344+
else:
345+
style = '' # empty string ignored
346+
ax = self.ax
347+
if self.style:
348+
style = self.style
349+
350+
plotf(ax, x, y, style, label=label, **self.kwds)
351+
ax.grid(self.grid)
352+
353+
def _maybe_convert_index(self, data):
354+
# tsplot converts automatically, but don't want to convert index
355+
# over and over for DataFrames
356+
from pandas.tseries.offsets import DateOffset
357+
from pandas.tseries.index import DatetimeIndex
358+
from pandas.core.frame import DataFrame
328359

329-
plotf = self._get_plot_function()
360+
if (isinstance(data.index, DatetimeIndex) and
361+
isinstance(data, DataFrame)):
362+
freq = getattr(data.index, 'freq', None)
363+
if freq is None and hasattr(data.index, 'inferred_freq'):
364+
freq = data.index.inferred_freq
330365

331-
for i, (label, y) in enumerate(self._iter_data()):
332-
if self.subplots:
333-
ax = self.axes[i]
334-
style = 'k'
366+
if isinstance(freq, DateOffset):
367+
freq = freq.rule_code
368+
369+
data = DataFrame(data.values,
370+
index=data.index.to_period(freq=freq),
371+
columns=data.columns)
372+
return data
373+
374+
def _make_ts_plot(self, data, **kwargs):
375+
from pandas.core.series import Series
376+
from pandas.core.frame import DataFrame
377+
import pandas.tseries.plotting as plot
378+
379+
if isinstance(data, Series):
380+
if self.subplots: # shouldn't even allow users to specify
381+
ax = self.axes[0]
335382
else:
336-
style = '' # empty string ignored
337383
ax = self.ax
338-
if self.style:
339-
style = self.style
340384

341-
plotf(ax, x, y, style, label=label, **self.kwds)
385+
label = com._stringify(self.label)
386+
plot.tsplot(ax, data, label=label, **kwargs)
342387
ax.grid(self.grid)
388+
else:
389+
for i, col in enumerate(data.columns):
390+
if self.subplots:
391+
ax = self.axes[i]
392+
else:
393+
ax = self.ax
394+
label = com._stringify(col)
395+
plot.tsplot(ax, data[col], label=label, **kwargs)
396+
ax.grid(self.grid)
343397

344398
def _post_plot_logic(self):
345399
df = self.data
@@ -602,24 +656,6 @@ def plot_series(series, label=None, kind='line', use_index=True, rot=None,
602656

603657
return plot_obj.ax
604658

605-
# if use_index:
606-
# # custom datetime/interval plotting
607-
# from pandas import IntervalIndex, DatetimeIndex
608-
# if isinstance(self.index, IntervalIndex):
609-
# return tsp.tsplot(self)
610-
# if isinstance(self.index, DatetimeIndex):
611-
# offset = self.index.freq
612-
# name = datetools._newOffsetNames.get(offset, None)
613-
# if name is not None:
614-
# try:
615-
# code = datetools._interval_str_to_code(name)
616-
# s_ = Series(self.values,
617-
# index=self.index.to_interval(freq=code),
618-
# name=self.name)
619-
# tsp.tsplot(s_)
620-
# except:
621-
# pass
622-
623659
def boxplot(data, column=None, by=None, ax=None, fontsize=None,
624660
rot=0, grid=True, figsize=None):
625661
"""

pandas/tseries/frequencies.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@
88
import pandas.tseries.offsets as offsets
99
import pandas._tseries as lib
1010

11+
class FreqGroup(object):
12+
FR_ANN = 1000
13+
FR_QTR = 2000
14+
FR_MTH = 3000
15+
FR_WK = 4000
16+
FR_BUS = 5000
17+
FR_DAY = 6000
18+
FR_HR = 7000
19+
FR_MIN = 8000
20+
FR_SEC = 9000
21+
FR_UND = -10000
22+
23+
def get_freq_group(freq):
24+
if isinstance(freq, basestring):
25+
base, mult = get_freq_code(freq)
26+
freq = base
27+
return (freq // 1000) * 1000
28+
29+
def get_freq(freq):
30+
if isinstance(freq, basestring):
31+
base, mult = get_freq_code(freq)
32+
freq = base
33+
return freq
1134

1235
def get_freq_code(freqstr):
1336
"""

0 commit comments

Comments
 (0)