@@ -33,6 +33,21 @@ def _skip_if_no_scipy():
3333 except ImportError :
3434 raise nose .SkipTest ("no scipy" )
3535
36+ def _skip_if_no_scipy_gaussian_kde ():
37+ try :
38+ import scipy
39+ from scipy .stats import gaussian_kde
40+ except ImportError :
41+ raise nose .SkipTest ("scipy version doesn't support gaussian_kde" )
42+
43+ def _ok_for_gaussian_kde (kind ):
44+ if kind in ['kde' ,'density' ]:
45+ try :
46+ import scipy
47+ from scipy .stats import gaussian_kde
48+ except ImportError :
49+ return False
50+ return True
3651
3752@tm .mplskip
3853class TestPlotBase (tm .TestCase ):
@@ -375,6 +390,8 @@ def test_plot(self):
375390 _check_plot_works (self .iseries .plot )
376391
377392 for kind in ['line' , 'bar' , 'barh' , 'kde' ]:
393+ if not _ok_for_gaussian_kde (kind ):
394+ continue
378395 _check_plot_works (self .series [:5 ].plot , kind = kind )
379396
380397 _check_plot_works (self .series [:10 ].plot , kind = 'barh' )
@@ -585,6 +602,7 @@ def test_plot_fails_when_ax_differs_from_figure(self):
585602 @slow
586603 def test_kde (self ):
587604 _skip_if_no_scipy ()
605+ _skip_if_no_scipy_gaussian_kde ()
588606 _check_plot_works (self .ts .plot , kind = 'kde' )
589607 _check_plot_works (self .ts .plot , kind = 'density' )
590608 ax = self .ts .plot (kind = 'kde' , logy = True )
@@ -593,6 +611,7 @@ def test_kde(self):
593611 @slow
594612 def test_kde_kwargs (self ):
595613 _skip_if_no_scipy ()
614+ _skip_if_no_scipy_gaussian_kde ()
596615 from numpy import linspace
597616 _check_plot_works (self .ts .plot , kind = 'kde' , bw_method = .5 , ind = linspace (- 100 ,100 ,20 ))
598617 _check_plot_works (self .ts .plot , kind = 'density' , bw_method = .5 , ind = linspace (- 100 ,100 ,20 ))
@@ -602,6 +621,7 @@ def test_kde_kwargs(self):
602621 @slow
603622 def test_kde_color (self ):
604623 _skip_if_no_scipy ()
624+ _skip_if_no_scipy_gaussian_kde ()
605625 ax = self .ts .plot (kind = 'kde' , logy = True , color = 'r' )
606626 self ._check_ax_scales (ax , yaxis = 'log' )
607627 lines = ax .get_lines ()
@@ -631,18 +651,24 @@ def test_bootstrap_plot(self):
631651 def test_invalid_plot_data (self ):
632652 s = Series (list ('abcd' ))
633653 for kind in plotting ._common_kinds :
654+ if not _ok_for_gaussian_kde (kind ):
655+ continue
634656 with tm .assertRaises (TypeError ):
635657 s .plot (kind = kind )
636658
637659 @slow
638660 def test_valid_object_plot (self ):
639661 s = Series (lrange (10 ), dtype = object )
640662 for kind in plotting ._common_kinds :
663+ if not _ok_for_gaussian_kde (kind ):
664+ continue
641665 _check_plot_works (s .plot , kind = kind )
642666
643667 def test_partially_invalid_plot_data (self ):
644668 s = Series (['a' , 'b' , 1.0 , 2 ])
645669 for kind in plotting ._common_kinds :
670+ if not _ok_for_gaussian_kde (kind ):
671+ continue
646672 with tm .assertRaises (TypeError ):
647673 s .plot (kind = kind )
648674
@@ -1341,7 +1367,7 @@ def test_boxplot(self):
13411367 self .assertRaisesRegexp (
13421368 ValueError , 'existing axis' , df .boxplot ,
13431369 column = ['Col1' , 'Col2' ], by = 'X' , ax = ax
1344- )
1370+ )
13451371
13461372 # When by is None, check that all relevant lines are present in the dict
13471373 fig , ax = self .plt .subplots ()
@@ -1425,6 +1451,7 @@ def test_boxplot_return_type_by(self):
14251451 @slow
14261452 def test_kde (self ):
14271453 _skip_if_no_scipy ()
1454+ _skip_if_no_scipy_gaussian_kde ()
14281455 df = DataFrame (randn (100 , 4 ))
14291456 ax = _check_plot_works (df .plot , kind = 'kde' )
14301457 expected = [com .pprint_thing (c ) for c in df .columns ]
@@ -1533,8 +1560,10 @@ def scat(**kwds):
15331560 _check_plot_works (scat )
15341561 _check_plot_works (scat , marker = '+' )
15351562 _check_plot_works (scat , vmin = 0 )
1536- _check_plot_works (scat , diagonal = 'kde' )
1537- _check_plot_works (scat , diagonal = 'density' )
1563+ if _ok_for_gaussian_kde ('kde' ):
1564+ _check_plot_works (scat , diagonal = 'kde' )
1565+ if _ok_for_gaussian_kde ('density' ):
1566+ _check_plot_works (scat , diagonal = 'density' )
15381567 _check_plot_works (scat , diagonal = 'hist' )
15391568 _check_plot_works (scat , range_padding = .1 )
15401569
@@ -1662,6 +1691,9 @@ def test_df_legend_labels(self):
16621691 df4 = DataFrame (rand (3 , 3 ), columns = ['j' , 'k' , 'l' ])
16631692
16641693 for kind in kinds :
1694+ if not _ok_for_gaussian_kde (kind ):
1695+ continue
1696+
16651697 ax = df .plot (kind = kind , legend = True )
16661698 self ._check_legend_labels (ax , labels = df .columns )
16671699
@@ -1734,6 +1766,9 @@ def test_no_legend(self):
17341766 df = DataFrame (rand (3 , 3 ), columns = ['a' , 'b' , 'c' ])
17351767
17361768 for kind in kinds :
1769+ if not _ok_for_gaussian_kde (kind ):
1770+ continue
1771+
17371772 ax = df .plot (kind = kind , legend = False )
17381773 self ._check_legend_labels (ax , visible = False )
17391774
@@ -1844,6 +1879,8 @@ def test_unordered_ts(self):
18441879 def test_all_invalid_plot_data (self ):
18451880 df = DataFrame (list ('abcd' ))
18461881 for kind in plotting ._common_kinds :
1882+ if not _ok_for_gaussian_kde (kind ):
1883+ continue
18471884 with tm .assertRaises (TypeError ):
18481885 df .plot (kind = kind )
18491886
@@ -1853,6 +1890,8 @@ def test_partially_invalid_plot_data(self):
18531890 df = DataFrame (randn (10 , 2 ), dtype = object )
18541891 df [np .random .rand (df .shape [0 ]) > 0.5 ] = 'a'
18551892 for kind in plotting ._common_kinds :
1893+ if not _ok_for_gaussian_kde (kind ):
1894+ continue
18561895 with tm .assertRaises (TypeError ):
18571896 df .plot (kind = kind )
18581897
0 commit comments