Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _aggregate(self, fields, features, year, ignore_missing):
aggregated_features = collections.defaultdict(list)
for _, feature in features:
if ignore_missing and None in feature.values():
continue
for field in fields:
aggregated_features[field].append(feature[field])
collapsed = {}
for field in fields:
if field.endswith('E'):
agg = sum(aggregated_features[field])
elif field.endswith('M'):
e_field = field[:-1] + 'E'
agg = census.math.moe_of_sum(aggregated_features[e_field],
aggregated_features[field])
else:
raise ValueError("Don't know how to aggregate this variable {}".format(feature))
collapsed[field] = agg
return collapsed