Skip to content

Commit

Permalink
Remove nans early in distplot to avoid reference rule errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaskom committed Jan 5, 2020
1 parent 930c6f4 commit 9c2ecd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 1 addition & 3 deletions seaborn/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ def distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None,
a = a.squeeze()

# Drop null values from array
mask = np.isnan(a)
if mask.any():
a = a[~mask]
a = remove_na(a)

# Decide if the hist is normed
norm_hist = norm_hist or kde or (fit is not None)
Expand Down
22 changes: 21 additions & 1 deletion seaborn/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@
_no_statsmodels = True


class TestDistPlot(object):

def test_distplot_with_nans(self):

f, (ax1, ax2) = plt.subplots(2)
x = np.random.randn(100)
x_null = np.append(x, [np.nan])

dist.distplot(x, ax=ax1)
dist.distplot(x_null, ax=ax2)

line1 = ax1.lines[0]
line2 = ax2.lines[0]
assert np.array_equal(line1.get_xydata(), line2.get_xydata())

for bar1, bar2 in zip(ax1.patches, ax2.patches):
assert bar1.get_xy() == bar2.get_xy()
assert bar1.get_height() == bar2.get_height()


class TestKDE(object):

rs = np.random.RandomState(0)
Expand Down Expand Up @@ -110,7 +130,7 @@ def test_kde_singular(self):
assert not line.get_xydata().size

@pytest.mark.parametrize("cumulative", [True, False])
def test_kdeplot_missing(self, cumulative):
def test_kdeplot_with_nans(self, cumulative):

x_missing = np.append(self.x, [np.nan, np.nan])

Expand Down

0 comments on commit 9c2ecd3

Please sign in to comment.