Skip to content

Commit 58e98a7

Browse files
committed
fix: Normalize decimals for to_json(key=...), closes wireservice/csvkit#1248
1 parent e6fc5be commit 58e98a7

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

CHANGELOG.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
1.11.0 - May 27, 2024
2+
---------------------
3+
4+
- 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".
5+
16
1.10.2 - April 28, 2024
27
-----------------------
38

agate/table/to_json.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
from collections import OrderedDict
4+
from decimal import Decimal
45

56

67
def to_json(self, path, key=None, newline=False, indent=None, **kwargs):
@@ -65,6 +66,8 @@ def dump_json(data):
6566
for row in self._rows:
6667
if key_is_row_function:
6768
k = key(row)
69+
elif isinstance(row[key], Decimal):
70+
k = str(row[key].normalize())
6871
else:
6972
k = str(row[key])
7073

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
project = 'agate'
1414
copyright = '2017, Christopher Groskopf'
15-
version = '1.10.2'
15+
version = '1.11.0'
1616
release = version
1717

1818
# -- General configuration ---------------------------------------------------

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='agate',
8-
version='1.10.2',
8+
version='1.11.0',
99
description='A data analysis library that is optimized for humans instead of machines.',
1010
long_description=long_description,
1111
long_description_content_type='text/x-rst',

tests/test_table/test_to_json.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import os
33
import sys
4+
from decimal import Decimal
45
from io import StringIO
56

67
from agate import Table
@@ -63,6 +64,16 @@ def test_to_json_non_string_key(self):
6364

6465
self.assertEqual(js1, js2)
6566

67+
def test_to_json_key_decimal(self):
68+
table = Table([[Decimal('3')], [Decimal('3.0')], [Decimal('3.00')]], ['a'], [Number()])
69+
70+
output = StringIO()
71+
72+
with self.assertRaises(ValueError) as exc:
73+
table.to_json(output, key='a', indent=4)
74+
75+
assert str(exc.exception) == 'Value 3 is not unique in the key column.'
76+
6677
def test_to_json_key_func(self):
6778
table = Table(self.rows, self.column_names, self.column_types)
6879

0 commit comments

Comments
 (0)