Skip to content

Commit 1fdae29

Browse files
committed
Add samply.py for example testing, modify get_values_for_csv to fix extra decimal points
1 parent 817b706 commit 1fdae29

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

pandas/core/indexes/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7758,10 +7758,11 @@ def get_values_for_csv(
77587758
if not quoting:
77597759
values = values.astype(str)
77607760
else:
7761-
values = np.array(values, dtype="object")
7761+
values = np.array(values, dtype="str") # Convert float16 -> string
7762+
values = values.astype(float, copy=False) # Parse string -> Python float64
77627763

7763-
values[mask] = na_rep
77647764
values = values.astype(object, copy=False)
7765+
values[mask] = na_rep
77657766
return values
77667767

77677768
from pandas.io.formats.format import FloatArrayFormatter

sample.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pandas as pd
2+
import numpy as np
3+
import csv
4+
5+
df = pd.DataFrame({"col": [8.5557]}, dtype=np.float32)
6+
7+
print(df.to_csv())
8+
9+
print(df.to_csv(quoting=csv.QUOTE_NONNUMERIC))
10+
11+
values = np.array([8.57], dtype="float32")
12+
print(values)
13+
# [8.57]
14+
15+
print(np.array(values, dtype="object"))
16+
# [8.569999694824219]
17+
18+
print(np.array(values, dtype="str"))
19+
20+
21+
# Original array in float32
22+
float32_arr = np.array([1.2345678, 2.3456789], dtype=np.float32)
23+
24+
# Convert to object
25+
object_arr = float32_arr.astype(object)
26+
27+
print("Original float32 array:", float32_arr)
28+
print("Object array:", object_arr)
29+
print("Data type of object_arr:", object_arr.dtype)

0 commit comments

Comments
 (0)