@@ -309,6 +309,11 @@ class LinePlot(MPLPlot):
309
309
310
310
def __init__ (self , data , ** kwargs ):
311
311
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
312
317
313
318
def _get_plot_function (self ):
314
319
if self .logy :
@@ -324,22 +329,71 @@ def _get_plot_function(self):
324
329
325
330
def _make_plot (self ):
326
331
# 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
328
359
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
330
365
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 ]
335
382
else :
336
- style = '' # empty string ignored
337
383
ax = self .ax
338
- if self .style :
339
- style = self .style
340
384
341
- plotf (ax , x , y , style , label = label , ** self .kwds )
385
+ label = com ._stringify (self .label )
386
+ plot .tsplot (ax , data , label = label , ** kwargs )
342
387
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 )
343
397
344
398
def _post_plot_logic (self ):
345
399
df = self .data
@@ -602,24 +656,6 @@ def plot_series(series, label=None, kind='line', use_index=True, rot=None,
602
656
603
657
return plot_obj .ax
604
658
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
-
623
659
def boxplot (data , column = None , by = None , ax = None , fontsize = None ,
624
660
rot = 0 , grid = True , figsize = None ):
625
661
"""
0 commit comments