Skip to content

Commit 52a5726

Browse files
author
Jessica Oraegbu
committed
Fixing format in floating point precision docs
1 parent 91fd754 commit 52a5726

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

doc/source/user_guide/io.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ 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
@@ -1686,20 +1686,20 @@ The conversion process is not always guaranteed to be accurate because small dif
16861686
``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.
16871687

16881688
.. ipython:: python
1689-
from import StringIO
1690-
import pandas as pd
16911689
1692-
x0 = 18292498239.824
1693-
df1 = pd.DataFrame({'One': [x0]}, index=["bignum"])
1690+
from io import StringIO
1691+
1692+
x0 = 18292498239.824
1693+
df1 = pd.DataFrame({'One': [x0]}, index=["bignum"])
16941694
1695-
csv_string = df1.to_csv(float_format='%.17g')
1696-
df2 = pd.read_csv(StringIO(csv_string), index_col=0, float_precision='round_trip')
1695+
csv_string = df1.to_csv(float_format='%.17g')
1696+
df2 = pd.read_csv(StringIO(csv_string), index_col=0, float_precision='round_trip')
16971697
1698-
x1 = df1.iloc[0, 0]
1699-
x2 = df2.iloc[0, 0]
1698+
x1 = df1.iloc[0, 0]
1699+
x2 = df2.iloc[0, 0]
17001700
1701-
print(f"x0 = {x0}; x1 = {x1}; Are they equal? {x0 == x1}")
1702-
print(f"x0 = {x0}; x2 = {x2}; Are they equal? {x0 == x2}")
1701+
print(f"x0 = {x0}; x1 = {x1}; Are they equal? {x0 == x1}")
1702+
print(f"x0 = {x0}; x2 = {x2}; Are they equal? {x0 == x2}")
17031703
17041704
Writing a formatted string
17051705
++++++++++++++++++++++++++

0 commit comments

Comments
 (0)