You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Floating Point Precision inaccuracies when writing and reading to CSV files happen due to how the numeric data is represented and parsed in pandas.
1678
1678
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
1679
1679
text values and converts them back into different types (floats, integers, strings) which is when the loss of float point precision happens.
1680
1680
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.
1681
1681
1682
1682
* ``float_format``: Format string for floating point numbers
1683
+
1683
1684
``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).
1684
1685
1685
1686
``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.
1686
1687
1687
1688
.. ipython:: python
1689
+
1688
1690
import pandas as pd
1689
1691
import os
1690
1692
@@ -1702,11 +1704,6 @@ The conversion process is not always guaranteed to be accurate because small dif
1702
1704
1703
1705
os.remove('test.csv')
1704
1706
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
0 commit comments