diff --git a/doc/source/whatsnew/v0.19.2.txt b/doc/source/whatsnew/v0.19.2.txt index 231297df3fb8f..caf195afc4966 100644 --- a/doc/source/whatsnew/v0.19.2.txt +++ b/doc/source/whatsnew/v0.19.2.txt @@ -79,4 +79,6 @@ Bug Fixes - Explicit check in ``to_stata`` and ``StataWriter`` for out-of-range values when writing doubles (:issue:`14618`) +- Bug in ``.plot(kind='kde')`` which did not drop missing values to generate the KDE Plot, instead generating an empty plot. (:issue:`14821`) + - Bug in ``unstack()`` if called with a list of column(s) as an argument, regardless of the dtypes of all columns, they get coerced to ``object`` (:issue:`11847`) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index e752197c6ad77..73119fec88198 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -569,7 +569,11 @@ def test_kde_missing_vals(self): _skip_if_no_scipy_gaussian_kde() s = Series(np.random.uniform(size=50)) s[0] = np.nan - _check_plot_works(s.plot.kde) + axes = _check_plot_works(s.plot.kde) + # check if the values have any missing values + # GH14821 + self.assertTrue(any(~np.isnan(axes.lines[0].get_xdata())), + msg='Missing Values not dropped') @slow def test_hist_kwargs(self): diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 21e8b64a3656a..bd9933b12b580 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -2153,9 +2153,10 @@ def _args_adjust(self): def _get_ind(self, y): if self.ind is None: - sample_range = max(y) - min(y) - ind = np.linspace(min(y) - 0.5 * sample_range, - max(y) + 0.5 * sample_range, 1000) + # np.nanmax() and np.nanmin() ignores the missing values + sample_range = np.nanmax(y) - np.nanmin(y) + ind = np.linspace(np.nanmin(y) - 0.5 * sample_range, + np.nanmax(y) + 0.5 * sample_range, 1000) else: ind = self.ind return ind