Skip to content

Commit 91fd754

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

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

doc/source/user_guide/io.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,25 +1686,21 @@ 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-
1689+
from import StringIO
16901690
import pandas as pd
1691-
import os
16921691
16931692
x0 = 18292498239.824
16941693
df1 = pd.DataFrame({'One': [x0]}, index=["bignum"])
16951694
1696-
df1.to_csv('test.csv', float_format='%.17g')
1697-
df2 = pd.read_csv('test.csv', 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')
16981697
16991698
x1 = df1.iloc[0, 0]
1700-
x2 = df2['One'][0]
1699+
x2 = df2.iloc[0, 0]
17011700
17021701
print(f"x0 = {x0}; x1 = {x1}; Are they equal? {x0 == x1}")
17031702
print(f"x0 = {x0}; x2 = {x2}; Are they equal? {x0 == x2}")
17041703
1705-
1706-
os.remove('test.csv')
1707-
17081704
Writing a formatted string
17091705
++++++++++++++++++++++++++
17101706

0 commit comments

Comments
 (0)