@@ -33,6 +33,21 @@ def _skip_if_no_scipy():
33
33
except ImportError :
34
34
raise nose .SkipTest ("no scipy" )
35
35
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
36
51
37
52
@tm .mplskip
38
53
class TestPlotBase (tm .TestCase ):
@@ -375,6 +390,8 @@ def test_plot(self):
375
390
_check_plot_works (self .iseries .plot )
376
391
377
392
for kind in ['line' , 'bar' , 'barh' , 'kde' ]:
393
+ if not _ok_for_gaussian_kde (kind ):
394
+ continue
378
395
_check_plot_works (self .series [:5 ].plot , kind = kind )
379
396
380
397
_check_plot_works (self .series [:10 ].plot , kind = 'barh' )
@@ -585,6 +602,7 @@ def test_plot_fails_when_ax_differs_from_figure(self):
585
602
@slow
586
603
def test_kde (self ):
587
604
_skip_if_no_scipy ()
605
+ _skip_if_no_scipy_gaussian_kde ()
588
606
_check_plot_works (self .ts .plot , kind = 'kde' )
589
607
_check_plot_works (self .ts .plot , kind = 'density' )
590
608
ax = self .ts .plot (kind = 'kde' , logy = True )
@@ -593,6 +611,7 @@ def test_kde(self):
593
611
@slow
594
612
def test_kde_kwargs (self ):
595
613
_skip_if_no_scipy ()
614
+ _skip_if_no_scipy_gaussian_kde ()
596
615
from numpy import linspace
597
616
_check_plot_works (self .ts .plot , kind = 'kde' , bw_method = .5 , ind = linspace (- 100 ,100 ,20 ))
598
617
_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):
602
621
@slow
603
622
def test_kde_color (self ):
604
623
_skip_if_no_scipy ()
624
+ _skip_if_no_scipy_gaussian_kde ()
605
625
ax = self .ts .plot (kind = 'kde' , logy = True , color = 'r' )
606
626
self ._check_ax_scales (ax , yaxis = 'log' )
607
627
lines = ax .get_lines ()
@@ -631,18 +651,24 @@ def test_bootstrap_plot(self):
631
651
def test_invalid_plot_data (self ):
632
652
s = Series (list ('abcd' ))
633
653
for kind in plotting ._common_kinds :
654
+ if not _ok_for_gaussian_kde (kind ):
655
+ continue
634
656
with tm .assertRaises (TypeError ):
635
657
s .plot (kind = kind )
636
658
637
659
@slow
638
660
def test_valid_object_plot (self ):
639
661
s = Series (lrange (10 ), dtype = object )
640
662
for kind in plotting ._common_kinds :
663
+ if not _ok_for_gaussian_kde (kind ):
664
+ continue
641
665
_check_plot_works (s .plot , kind = kind )
642
666
643
667
def test_partially_invalid_plot_data (self ):
644
668
s = Series (['a' , 'b' , 1.0 , 2 ])
645
669
for kind in plotting ._common_kinds :
670
+ if not _ok_for_gaussian_kde (kind ):
671
+ continue
646
672
with tm .assertRaises (TypeError ):
647
673
s .plot (kind = kind )
648
674
@@ -1341,7 +1367,7 @@ def test_boxplot(self):
1341
1367
self .assertRaisesRegexp (
1342
1368
ValueError , 'existing axis' , df .boxplot ,
1343
1369
column = ['Col1' , 'Col2' ], by = 'X' , ax = ax
1344
- )
1370
+ )
1345
1371
1346
1372
# When by is None, check that all relevant lines are present in the dict
1347
1373
fig , ax = self .plt .subplots ()
@@ -1425,6 +1451,7 @@ def test_boxplot_return_type_by(self):
1425
1451
@slow
1426
1452
def test_kde (self ):
1427
1453
_skip_if_no_scipy ()
1454
+ _skip_if_no_scipy_gaussian_kde ()
1428
1455
df = DataFrame (randn (100 , 4 ))
1429
1456
ax = _check_plot_works (df .plot , kind = 'kde' )
1430
1457
expected = [com .pprint_thing (c ) for c in df .columns ]
@@ -1533,8 +1560,10 @@ def scat(**kwds):
1533
1560
_check_plot_works (scat )
1534
1561
_check_plot_works (scat , marker = '+' )
1535
1562
_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' )
1538
1567
_check_plot_works (scat , diagonal = 'hist' )
1539
1568
_check_plot_works (scat , range_padding = .1 )
1540
1569
@@ -1662,6 +1691,9 @@ def test_df_legend_labels(self):
1662
1691
df4 = DataFrame (rand (3 , 3 ), columns = ['j' , 'k' , 'l' ])
1663
1692
1664
1693
for kind in kinds :
1694
+ if not _ok_for_gaussian_kde (kind ):
1695
+ continue
1696
+
1665
1697
ax = df .plot (kind = kind , legend = True )
1666
1698
self ._check_legend_labels (ax , labels = df .columns )
1667
1699
@@ -1734,6 +1766,9 @@ def test_no_legend(self):
1734
1766
df = DataFrame (rand (3 , 3 ), columns = ['a' , 'b' , 'c' ])
1735
1767
1736
1768
for kind in kinds :
1769
+ if not _ok_for_gaussian_kde (kind ):
1770
+ continue
1771
+
1737
1772
ax = df .plot (kind = kind , legend = False )
1738
1773
self ._check_legend_labels (ax , visible = False )
1739
1774
@@ -1844,6 +1879,8 @@ def test_unordered_ts(self):
1844
1879
def test_all_invalid_plot_data (self ):
1845
1880
df = DataFrame (list ('abcd' ))
1846
1881
for kind in plotting ._common_kinds :
1882
+ if not _ok_for_gaussian_kde (kind ):
1883
+ continue
1847
1884
with tm .assertRaises (TypeError ):
1848
1885
df .plot (kind = kind )
1849
1886
@@ -1853,6 +1890,8 @@ def test_partially_invalid_plot_data(self):
1853
1890
df = DataFrame (randn (10 , 2 ), dtype = object )
1854
1891
df [np .random .rand (df .shape [0 ]) > 0.5 ] = 'a'
1855
1892
for kind in plotting ._common_kinds :
1893
+ if not _ok_for_gaussian_kde (kind ):
1894
+ continue
1856
1895
with tm .assertRaises (TypeError ):
1857
1896
df .plot (kind = kind )
1858
1897
0 commit comments