Skip to content

Commit c9710ee

Browse files
committed
add more tests for examples listed in issue pandas-dev#15716 and pandas-dev#15864
1 parent 3cee6b3 commit c9710ee

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/tests/io/json/test_pandas.py

+25
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,31 @@ def test_frame_from_json_nones(self):
380380
unser = read_json(df.to_json(), dtype=False)
381381
self.assertTrue(np.isnan(unser[2][0]))
382382

383+
def test_frame_to_json_float_precision(self):
384+
df = pd.DataFrame([dict(a_float=0.95)])
385+
encoded = df.to_json(double_precision=1)
386+
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')
387+
388+
df = pd.DataFrame([dict(a_float=1.95)])
389+
encoded = df.to_json(double_precision=1)
390+
self.assertEqual(encoded, '{"a_float":{"0":2.0}}')
391+
392+
df = pd.DataFrame([dict(a_float=-1.95)])
393+
encoded = df.to_json(double_precision=1)
394+
self.assertEqual(encoded, '{"a_float":{"0":-2.0}}')
395+
396+
df = pd.DataFrame([dict(a_float=0.995)])
397+
encoded = df.to_json(double_precision=2)
398+
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')
399+
400+
df = pd.DataFrame([dict(a_float=0.9995)])
401+
encoded = df.to_json(double_precision=3)
402+
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')
403+
404+
df = pd.DataFrame([dict(a_float=0.99999999999999944)])
405+
encoded = df.to_json(double_precision=15)
406+
self.assertEqual(encoded, '{"a_float":{"0":1.0}}')
407+
383408
def test_frame_to_json_except(self):
384409
df = DataFrame([1, 2, 3])
385410
self.assertRaises(ValueError, df.to_json, orient="garbage")

0 commit comments

Comments
 (0)