Skip to content

Commit

Permalink
fix: Normalize decimals for to_json(key=...), closes wireservice/csvk…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed May 27, 2024
1 parent e6fc5be commit 58e98a7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.11.0 - May 27, 2024
---------------------

- fix: The `key` argument to :meth:`.Table.to_json` errors if two values are equal, even if their CSV representation is different: for example, "1/1/2020" and "01/01/2020". However, until now, this was not the case for numbers: for example, "3.0" was treated as unequal to "3.00".

1.10.2 - April 28, 2024
-----------------------

Expand Down
3 changes: 3 additions & 0 deletions agate/table/to_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
from collections import OrderedDict
from decimal import Decimal


def to_json(self, path, key=None, newline=False, indent=None, **kwargs):
Expand Down Expand Up @@ -65,6 +66,8 @@ def dump_json(data):
for row in self._rows:
if key_is_row_function:
k = key(row)
elif isinstance(row[key], Decimal):
k = str(row[key].normalize())
else:
k = str(row[key])

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

project = 'agate'
copyright = '2017, Christopher Groskopf'
version = '1.10.2'
version = '1.11.0'
release = version

# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='agate',
version='1.10.2',
version='1.11.0',
description='A data analysis library that is optimized for humans instead of machines.',
long_description=long_description,
long_description_content_type='text/x-rst',
Expand Down
11 changes: 11 additions & 0 deletions tests/test_table/test_to_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import sys
from decimal import Decimal
from io import StringIO

from agate import Table
Expand Down Expand Up @@ -63,6 +64,16 @@ def test_to_json_non_string_key(self):

self.assertEqual(js1, js2)

def test_to_json_key_decimal(self):
table = Table([[Decimal('3')], [Decimal('3.0')], [Decimal('3.00')]], ['a'], [Number()])

output = StringIO()

with self.assertRaises(ValueError) as exc:
table.to_json(output, key='a', indent=4)

assert str(exc.exception) == 'Value 3 is not unique in the key column.'

def test_to_json_key_func(self):
table = Table(self.rows, self.column_names, self.column_types)

Expand Down

0 comments on commit 58e98a7

Please sign in to comment.