File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -7758,10 +7758,11 @@ def get_values_for_csv(
7758
7758
if not quoting :
7759
7759
values = values .astype (str )
7760
7760
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
7762
7763
7763
- values [mask ] = na_rep
7764
7764
values = values .astype (object , copy = False )
7765
+ values [mask ] = na_rep
7765
7766
return values
7766
7767
7767
7768
from pandas .io .formats .format import FloatArrayFormatter
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments