Skip to content

Commit a5410c1

Browse files
author
Jessica Oraegbu
committed
Fixing format in floating point precision docs
1 parent e1d0119 commit a5410c1

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

doc/source/user_guide/io.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,19 +1672,21 @@ function takes a number of arguments. Only the first is required.
16721672
* ``date_format``: Format string for datetime objects
16731673

16741674
Floating Point Precision on Writing and Reading to CSV Files
1675-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1675+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16761676

16771677
Floating Point Precision inaccuracies when writing and reading to CSV files happen due to how the numeric data is represented and parsed in pandas.
16781678
During the write process, pandas converts all the numeric values into text that is stored as bytes in the CSV file. However, when we read the CSV back, pandas parses those
16791679
text values and converts them back into different types (floats, integers, strings) which is when the loss of float point precision happens.
16801680
The conversion process is not always guaranteed to be accurate because small differences in data representation between original and reloaded data frame can occur leading to precision loss.
16811681

16821682
* ``float_format``: Format string for floating point numbers
1683+
16831684
``df.to_csv('file.csv', float_format='%.17g')`` allows for floating point precision to be specified when writing to the CSV file. In this example, this ensures that the floating point is written in this exact format of 17 significant digits (64-bit float).
16841685

16851686
``df = pd.read_csv('file.csv', float_precision='round_trip')`` allows for floating point precision to be specified when reading from the CSV file. This is guaranteed to round-trip values after writing to a file and Pandas will read the numbers without losing or changing decimal places.
16861687

16871688
.. ipython:: python
1689+
16881690
import pandas as pd
16891691
import os
16901692
@@ -1702,11 +1704,6 @@ The conversion process is not always guaranteed to be accurate because small dif
17021704
17031705
os.remove('test.csv')
17041706
1705-
.. Output received:
1706-
x0 = 18292498239.824001; x1 = 18292498239.824001; Are they equal? True
1707-
x0 = 18292498239.824001; x2 = 18292498239.824001; Are they equal? True
1708-
..
1709-
17101707
Writing a formatted string
17111708
++++++++++++++++++++++++++
17121709

0 commit comments

Comments
 (0)