File tree Expand file tree Collapse file tree 2 files changed +3
-10
lines changed Expand file tree Collapse file tree 2 files changed +3
-10
lines changed Original file line number Diff line number Diff line change 19
19
)
20
20
21
21
import numpy as np
22
+ import json
22
23
23
24
from pandas ._libs import writers as libwriters
24
25
from pandas ._typing import SequenceNotStr
@@ -100,12 +101,7 @@ def __init__(
100
101
self .cols = self ._initialize_columns (cols )
101
102
self .chunksize = self ._initialize_chunksize (chunksize )
102
103
103
- print ("output preserve var: " , self .preserve_complex )
104
104
if self .preserve_complex :
105
- print ("here1" )
106
- import json
107
- import numpy as np
108
-
109
105
for col in self .obj .columns :
110
106
if self .obj [col ].dtype == "O" :
111
107
first_val = self .obj [col ].iloc [0 ]
Original file line number Diff line number Diff line change @@ -863,22 +863,19 @@ def read_csv(
863
863
864
864
def _restore_complex_arrays (df : DataFrame ) -> None :
865
865
"""
866
- Loop over each column of df, check if it contains bracketed JSON strings
867
- like "[0.1, 0.2, 0.3]", and parse them back into NumPy arrays .
866
+ Converted bracketed JSON strings in df back to NumPy arrays.
867
+ eg. "[0.1, 0.2, 0.3]" --> parse into NumPy array .
868
868
"""
869
869
def looks_like_json_array (x : str ) -> bool :
870
870
return x .startswith ("[" ) and x .endswith ("]" )
871
871
872
872
for col in df .columns :
873
- # Only parse object columns
874
873
if df [col ].dtype == "object" :
875
- # skip null
876
874
nonnull = df [col ].dropna ()
877
875
if (
878
876
len (nonnull ) > 0
879
877
and nonnull .apply (lambda x : isinstance (x , str ) and looks_like_json_array (x )).all ()
880
878
):
881
- # parse
882
879
df [col ] = df [col ].apply (lambda x : np .array (json .loads (x )) if pd .notnull (x ) else x )
883
880
884
881
You can’t perform that action at this time.
0 commit comments