|
1 | 1 | from datetime import datetime, date
|
2 | 2 | from unittest.mock import patch
|
3 | 3 |
|
4 |
| -from delphi_hhs.run import _date_to_int, int_date_to_previous_day_datetime, generate_date_ranges, \ |
| 4 | +from delphi_hhs.run import _date_to_int, add_nancodes, int_date_to_previous_day_datetime, generate_date_ranges, \ |
5 | 5 | make_signal, make_geo, run_module
|
6 | 6 | from delphi_hhs.constants import CONFIRMED, SUM_CONF_SUSP
|
7 |
| -from delphi_utils.geomap import GeoMapper |
| 7 | +from delphi_utils import GeoMapper, Nans |
8 | 8 | from freezegun import freeze_time
|
9 | 9 | import numpy as np
|
10 | 10 | import pandas as pd
|
@@ -72,38 +72,31 @@ def test_make_geo():
|
72 | 72 | """Check that geographies transform correctly."""
|
73 | 73 | test_timestamp = datetime(year=2020, month=1, day=1)
|
74 | 74 | geo_mapper = GeoMapper()
|
75 |
| - |
| 75 | + |
76 | 76 | data = pd.DataFrame({
|
77 | 77 | 'state': ['PA','WV','OH'],
|
78 | 78 | 'state_code': [42, 54, 39],
|
79 | 79 | 'timestamp': [test_timestamp]*3,
|
80 | 80 | 'val': [1, 2, 4],
|
81 | 81 | })
|
82 | 82 |
|
83 |
| - template = { |
84 |
| - 'se': np.nan, |
85 |
| - 'sample_size': np.nan, |
86 |
| - } |
87 | 83 | expecteds = {
|
88 | 84 | "state": pd.DataFrame(
|
89 |
| - dict(template, |
90 |
| - geo_id=data.state, |
| 85 | + dict(geo_id=data.state, |
91 | 86 | timestamp=data.timestamp,
|
92 | 87 | val=data.val)),
|
93 | 88 | "hhs": pd.DataFrame(
|
94 |
| - dict(template, |
95 |
| - geo_id=['3', '5'], |
| 89 | + dict(geo_id=['3', '5'], |
96 | 90 | timestamp=[test_timestamp]*2,
|
97 | 91 | val=[3, 4])),
|
98 | 92 | "nation": pd.DataFrame(
|
99 |
| - dict(template, |
100 |
| - geo_id=['us'], |
| 93 | + dict(geo_id=['us'], |
101 | 94 | timestamp=[test_timestamp],
|
102 | 95 | val=[7]))
|
103 | 96 | }
|
104 | 97 | for geo, expected in expecteds.items():
|
105 | 98 | result = make_geo(data, geo, geo_mapper)
|
106 |
| - for series in ["geo_id", "timestamp", "val", "se", "sample_size"]: |
| 99 | + for series in ["geo_id", "timestamp", "val"]: |
107 | 100 | pd.testing.assert_series_equal(expected[series], result[series], obj=f"{geo}:{series}")
|
108 | 101 |
|
109 | 102 |
|
@@ -131,3 +124,25 @@ def test_ignore_last_range_no_results(mock_covid_hosp, mock_export):
|
131 | 124 | }
|
132 | 125 | }
|
133 | 126 | assert not run_module(params) # function should not raise value error and has no return value
|
| 127 | + |
| 128 | +def test_add_nancode(): |
| 129 | + data = pd.DataFrame({ |
| 130 | + 'state': ['PA','WV','OH'], |
| 131 | + 'state_code': [42, 54, 39], |
| 132 | + 'timestamp': [pd.to_datetime("20200601")]*3, |
| 133 | + 'val': [1, 2, np.nan], |
| 134 | + 'se': [np.nan] * 3, |
| 135 | + 'sample_size': [np.nan] * 3, |
| 136 | + }) |
| 137 | + expected = pd.DataFrame({ |
| 138 | + 'state': ['PA','WV','OH'], |
| 139 | + 'state_code': [42, 54, 39], |
| 140 | + 'timestamp': [pd.to_datetime("20200601")]*3, |
| 141 | + 'val': [1, 2, np.nan], |
| 142 | + 'se': [np.nan] * 3, |
| 143 | + 'sample_size': [np.nan] * 3, |
| 144 | + 'missing_val': [Nans.NOT_MISSING] * 2 + [Nans.UNKNOWN], |
| 145 | + 'missing_se': [Nans.NOT_APPLICABLE] * 3, |
| 146 | + 'missing_sample_size': [Nans.NOT_APPLICABLE] * 3, |
| 147 | + }) |
| 148 | + pd.testing.assert_frame_equal(expected, add_nancodes(data)) |
0 commit comments