Skip to content

Commit

Permalink
Fix bug where we would convert int "id" groups to float
Browse files Browse the repository at this point in the history
The problem was that Pandas' iterrows() converted int columns to float. The
solution was using itertuples() instead.

Check pandas-dev/pandas#6509
  • Loading branch information
vitorbaptista committed Apr 8, 2015
1 parent a198dce commit 3b49bba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pipeline/metrics/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ def main(self, csv_path,
if groupby:
votes.insert(0, groupby, votes.index)

rows = []
columns = votes.columns.tolist()
replace_nan_with_none = lambda df: df.where(pd.notnull(df), None)
rows = [collections.OrderedDict(replace_nan_with_none(row))
for i, row in votes.iterrows()]
for row in replace_nan_with_none(votes).itertuples(False):
rows.append(collections.OrderedDict(zip(columns, row)))
return rows

def calculate_metric(self, votes, metric_method):
Expand Down
8 changes: 8 additions & 0 deletions pipeline/test/metrics/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io
import csv
import collections
import numpy as np

from pipeline.metrics.runner import Runner
from pipeline.metrics.runner import Rollcall
Expand Down Expand Up @@ -81,6 +82,13 @@ def test_main_groups_correctly_when_not_calculating_metric(self):
groupby='party')
self.assertEqual(res, expected_result)

def test_main_groups_doesnt_convert_int_groups_to_float(self):
csv_path = self._get_csv_path('example_votes_with_metadata.csv')

res = Runner().main(csv_path, metric_method=None,
groupby='id', name='Joao')
self.assertEqual(type(res[0]['id']), np.int64)

def test_remove_unanimous_votes_runs_before_filters(self):
csv_path = self._get_csv_path('example_votes_with_metadata.csv')
expected_result = [collections.OrderedDict([
Expand Down

0 comments on commit 3b49bba

Please sign in to comment.