Skip to content

Commit 28be430

Browse files
committed
Make linter happy
1 parent 5f053ee commit 28be430

File tree

1 file changed

+23
-16
lines changed
  • usafacts/delphi_usafacts

1 file changed

+23
-16
lines changed

usafacts/delphi_usafacts/run.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@
6565
]
6666

6767

68+
def add_nancodes(df, smoother):
69+
"""Add nancodes to the dataframe."""
70+
idx = pd.IndexSlice
71+
72+
# Default nancodes
73+
df["missing_val"] = Nans.NOT_MISSING
74+
df["missing_se"] = Nans.NOT_APPLICABLE
75+
df["missing_sample_size"] = Nans.NOT_APPLICABLE
76+
77+
# Mark early smoothing entries as data insufficient
78+
if smoother == "seven_day_average":
79+
df.sort_index(inplace=True)
80+
min_time_value = df.index.min()[0] + 6 * pd.Timedelta(days=1)
81+
df.loc[idx[:min_time_value, :], "missing_val"] = Nans.DATA_INSUFFICIENT
82+
83+
# Mark any remaining nans with unknown
84+
remaining_nans_mask = df["val"].isnull() & (df["missing_val"] == Nans.NOT_MISSING)
85+
df.loc[remaining_nans_mask, "missing_val"] = Nans.UNKNOWN
86+
return df
87+
88+
6889
def run_module(params: Dict[str, Dict[str, Any]]):
6990
"""Run the usafacts indicator.
7091
@@ -114,30 +135,16 @@ def run_module(params: Dict[str, Dict[str, Any]]):
114135
# Aggregate to appropriate geographic resolution
115136
df = geo_map(df, geo_res, sensor)
116137
df.set_index(["timestamp", "geo_id"], inplace=True)
117-
idx = pd.IndexSlice
118138

119139
# Smooth
120-
smooth_obj, smoother_prefix, is_smooth_wip, smoother_lag = SMOOTHERS_MAP[smoother]
140+
smooth_obj, smoother_prefix, _, smoother_lag = SMOOTHERS_MAP[smoother]
121141
df["val"] = df[sensor].groupby(level=1).transform(smooth_obj.smooth)
122142

123143
# USAFacts is not a survey indicator
124144
df["se"] = np.nan
125145
df["sample_size"] = np.nan
126146

127-
# Default missing code
128-
df["missing_val"] = Nans.NOT_MISSING
129-
df["missing_se"] = Nans.NOT_APPLICABLE
130-
df["missing_sample_size"] = Nans.NOT_APPLICABLE
131-
132-
# Mark early smoothing entries as data insufficient
133-
if smoother == "seven_day_average":
134-
df.sort_index(inplace=True)
135-
min_time_value = df.index.min()[0] + 6 * pd.Timedelta(days=1)
136-
df.loc[idx[:min_time_value, :], "missing_val"] = Nans.DATA_INSUFFICIENT
137-
138-
# Mark any remaining nans with unknown
139-
remaining_nans_mask = df["val"].isnull() & (df["missing_val"] == Nans.NOT_MISSING)
140-
df.loc[remaining_nans_mask, "missing_val"] = Nans.UNKNOWN
147+
df = add_nancodes(df, smoother)
141148

142149
df.reset_index(inplace=True)
143150
sensor_name = SENSOR_NAME_MAP[sensor][0]

0 commit comments

Comments
 (0)