From a67c6b3b62e2879feea0cd20ba968e1de57fb3ee Mon Sep 17 00:00:00 2001 From: Lukas Koch Date: Sun, 21 Mar 2021 20:55:50 +0000 Subject: [PATCH] Deal with NaNs in the histograms. --- histoprint/formatter.py | 2 +- tests/test.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/histoprint/formatter.py b/histoprint/formatter.py index 8ebd012..1a1848f 100644 --- a/histoprint/formatter.py +++ b/histoprint/formatter.py @@ -411,7 +411,7 @@ def format_histogram(self, counts): ) hist_width = self.columns - axis_width - counts = np.array(counts) + counts = np.nan_to_num(np.array(counts)) while counts.ndim < 2: # Make sure counts is a 2D array counts = counts[np.newaxis, ...] diff --git a/tests/test.py b/tests/test.py index 9752924..af1b234 100644 --- a/tests/test.py +++ b/tests/test.py @@ -47,6 +47,15 @@ def test_hist(): ) +def test_nan(): + """Test dealing with nan values.""" + + data = np.arange(7, dtype=float) + data[5] = np.nan + bins = np.arange(8) + print_hist((data, bins)) + + def test_boost(): """Test boost-histogram if it is available."""