Skip to content

BUG: Fix rollover handling in json encoding #15865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ I/O
- Bug in ``pd.read_hdf()`` passing a ``Timestamp`` to the ``where`` parameter with a non date column (:issue:`15492`)
- Bug in ``DataFrame.to_stata()`` and ``StataWriter`` which produces incorrectly formatted files to be produced for some locales (:issue:`13856`)
- Bug in ``StataReader`` and ``StataWriter`` which allows invalid encodings (:issue:`15723`)
- Bug in ``pd.to_json()`` for the C engine where rollover was not correctly handled for case where frac is odd and diff is exactly 0.5 (:issue:`15716`, :issue:`15864`)

Plotting
^^^^^^^^
Expand Down
12 changes: 7 additions & 5 deletions pandas/_libs/src/ujson/lib/ultrajsonenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,17 +823,19 @@ int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc,

if (diff > 0.5) {
++frac;
/* handle rollover, e.g. case 0.99 with prec 1 is 1.0 */
if (frac >= pow10) {
frac = 0;
++whole;
}
} else if (diff == 0.5 && ((frac == 0) || (frac & 1))) {
/* if halfway, round up if odd, OR
if last digit is 0. That last part is strange */
++frac;
}

// handle rollover, e.g.
// case 0.99 with prec 1 is 1.0 and case 0.95 with prec is 1.0 as well
if (frac >= pow10) {
frac = 0;
++whole;
}

if (enc->doublePrecision == 0) {
diff = value - whole;

Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,31 @@ def test_frame_from_json_nones(self):
unser = read_json(df.to_json(), dtype=False)
self.assertTrue(np.isnan(unser[2][0]))

def test_frame_to_json_float_precision(self):
df = pd.DataFrame([dict(a_float=0.95)])
encoded = df.to_json(double_precision=1)
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')

df = pd.DataFrame([dict(a_float=1.95)])
encoded = df.to_json(double_precision=1)
self.assertEqual(encoded, '{"a_float":{"0":2.0}}')

df = pd.DataFrame([dict(a_float=-1.95)])
encoded = df.to_json(double_precision=1)
self.assertEqual(encoded, '{"a_float":{"0":-2.0}}')

df = pd.DataFrame([dict(a_float=0.995)])
encoded = df.to_json(double_precision=2)
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')

df = pd.DataFrame([dict(a_float=0.9995)])
encoded = df.to_json(double_precision=3)
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')

df = pd.DataFrame([dict(a_float=0.99999999999999944)])
encoded = df.to_json(double_precision=15)
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')

def test_frame_to_json_except(self):
df = DataFrame([1, 2, 3])
self.assertRaises(ValueError, df.to_json, orient="garbage")
Expand Down
42 changes: 42 additions & 0 deletions pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,48 @@ def test_encodeDecimal(self):
decoded = ujson.decode(encoded)
self.assertEqual(decoded, 1337.1337)

sut = decimal.Decimal("0.95")
encoded = ujson.encode(sut, double_precision=1)
self.assertEqual(encoded, "1.0")
decoded = ujson.decode(encoded)
self.assertEqual(decoded, 1.0)

sut = decimal.Decimal("0.94")
encoded = ujson.encode(sut, double_precision=1)
self.assertEqual(encoded, "0.9")
decoded = ujson.decode(encoded)
self.assertEqual(decoded, 0.9)

sut = decimal.Decimal("1.95")
encoded = ujson.encode(sut, double_precision=1)
self.assertEqual(encoded, "2.0")
decoded = ujson.decode(encoded)
self.assertEqual(decoded, 2.0)

sut = decimal.Decimal("-1.95")
encoded = ujson.encode(sut, double_precision=1)
self.assertEqual(encoded, "-2.0")
decoded = ujson.decode(encoded)
self.assertEqual(decoded, -2.0)

sut = decimal.Decimal("0.995")
encoded = ujson.encode(sut, double_precision=2)
self.assertEqual(encoded, "1.0")
decoded = ujson.decode(encoded)
self.assertEqual(decoded, 1.0)

sut = decimal.Decimal("0.9995")
encoded = ujson.encode(sut, double_precision=3)
self.assertEqual(encoded, "1.0")
decoded = ujson.decode(encoded)
self.assertEqual(decoded, 1.0)

sut = decimal.Decimal("0.99999999999999944")
encoded = ujson.encode(sut, double_precision=15)
self.assertEqual(encoded, "1.0")
decoded = ujson.decode(encoded)
self.assertEqual(decoded, 1.0)

def test_encodeStringConversion(self):
input = "A string \\ / \b \f \n \r \t </script> &"
not_html_encoded = ('"A string \\\\ \\/ \\b \\f \\n '
Expand Down