|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import numpy as np |
| 4 | +import pandas as pd |
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from histogrammar.dfinterface.pandas_histogrammar import PandasHistogrammar |
@@ -227,3 +228,56 @@ def test_get_histograms_module(): |
227 | 228 | def test_get_time_axes(): |
228 | 229 | time_axes = get_time_axes(pytest.test_df) |
229 | 230 | np.testing.assert_array_equal(time_axes, ["date"]) |
| 231 | + |
| 232 | + |
| 233 | +def test_null_histograms(): |
| 234 | + d = {'transaction': {0: np.nan, 1: 1.0, 2: np.nan, 3: 3.0, 4: 4.0}, |
| 235 | + 'isActive': {0: None, 1: None, 2: True, 3: True, 4: False}, |
| 236 | + 'eyeColor': {0: None, 1: None, 2: 'Jones', 3: 'USA', 4: 'FL'}, |
| 237 | + 't2': {0: np.nan, 1: 2.0, 2: np.nan, 3: 4.0, 4: 5.0}, |
| 238 | + 'foo': {0: np.nan, 1: np.nan, 2: np.nan, 3: True, 4: False}, |
| 239 | + 'bar': {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}, |
| 240 | + 'bla': {0: 1, 1: 2, 2: 3, 3: 4, 4: np.nan}, |
| 241 | + 'mixed': {0: 'a', 1: 'b', 2: 'c', 3: np.nan, 4: 1}} |
| 242 | + df = pd.DataFrame(d) |
| 243 | + df['bar'] = df['bar'].astype('category') |
| 244 | + |
| 245 | + hists = make_histograms(df, bin_specs={'transaction': {'num': 40, 'low': 0, 'high': 10}}) |
| 246 | + |
| 247 | + assert 'transaction' in hists |
| 248 | + assert 'isActive' in hists |
| 249 | + assert 'eyeColor' in hists |
| 250 | + assert 't2' in hists |
| 251 | + assert 'foo' in hists |
| 252 | + assert 'bar' in hists |
| 253 | + assert 'bla' in hists |
| 254 | + assert 'bla' in hists |
| 255 | + assert 'mixed' in hists |
| 256 | + |
| 257 | + h = hists['transaction'] |
| 258 | + assert h.nanflow.entries == 2 |
| 259 | + |
| 260 | + h = hists['t2'] |
| 261 | + assert h.nanflow.entries == 2 |
| 262 | + |
| 263 | + h = hists['isActive'] |
| 264 | + assert 'NaN' in h.bins |
| 265 | + assert h.bins['NaN'].entries == 2 |
| 266 | + |
| 267 | + h = hists['eyeColor'] |
| 268 | + assert 'None' in h.bins |
| 269 | + assert h.bins['None'].entries == 2 |
| 270 | + |
| 271 | + h = hists['foo'] |
| 272 | + assert 'NaN' in h.bins |
| 273 | + assert h.bins['NaN'].entries == 3 |
| 274 | + |
| 275 | + h = hists['bar'] |
| 276 | + assert 'NaN' not in h.bins |
| 277 | + |
| 278 | + h = hists['bla'] |
| 279 | + assert h.nanflow.entries == 1 |
| 280 | + |
| 281 | + h = hists['mixed'] |
| 282 | + assert 'nan' in h.bins |
| 283 | + assert h.bins['nan'].entries == 1 |
0 commit comments