Skip to content

Commit 3b49bba

Browse files
committed
Fix bug where we would convert int "id" groups to float
The problem was that Pandas' iterrows() converted int columns to float. The solution was using itertuples() instead. Check pandas-dev/pandas#6509
1 parent a198dce commit 3b49bba

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pipeline/metrics/runner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ def main(self, csv_path,
7373
if groupby:
7474
votes.insert(0, groupby, votes.index)
7575

76+
rows = []
77+
columns = votes.columns.tolist()
7678
replace_nan_with_none = lambda df: df.where(pd.notnull(df), None)
77-
rows = [collections.OrderedDict(replace_nan_with_none(row))
78-
for i, row in votes.iterrows()]
79+
for row in replace_nan_with_none(votes).itertuples(False):
80+
rows.append(collections.OrderedDict(zip(columns, row)))
7981
return rows
8082

8183
def calculate_metric(self, votes, metric_method):

pipeline/test/metrics/test_runner.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io
66
import csv
77
import collections
8+
import numpy as np
89

910
from pipeline.metrics.runner import Runner
1011
from pipeline.metrics.runner import Rollcall
@@ -81,6 +82,13 @@ def test_main_groups_correctly_when_not_calculating_metric(self):
8182
groupby='party')
8283
self.assertEqual(res, expected_result)
8384

85+
def test_main_groups_doesnt_convert_int_groups_to_float(self):
86+
csv_path = self._get_csv_path('example_votes_with_metadata.csv')
87+
88+
res = Runner().main(csv_path, metric_method=None,
89+
groupby='id', name='Joao')
90+
self.assertEqual(type(res[0]['id']), np.int64)
91+
8492
def test_remove_unanimous_votes_runs_before_filters(self):
8593
csv_path = self._get_csv_path('example_votes_with_metadata.csv')
8694
expected_result = [collections.OrderedDict([

0 commit comments

Comments
 (0)