Skip to content

Commit 798c1f2

Browse files
committed
Clean up
1 parent 2cac1bb commit 798c1f2

File tree

2 files changed

+3
-10
lines changed

2 files changed

+3
-10
lines changed

pandas/io/formats/csvs.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020

2121
import numpy as np
22+
import json
2223

2324
from pandas._libs import writers as libwriters
2425
from pandas._typing import SequenceNotStr
@@ -100,12 +101,7 @@ def __init__(
100101
self.cols = self._initialize_columns(cols)
101102
self.chunksize = self._initialize_chunksize(chunksize)
102103

103-
print("output preserve var: ", self.preserve_complex)
104104
if self.preserve_complex:
105-
print("here1")
106-
import json
107-
import numpy as np
108-
109105
for col in self.obj.columns:
110106
if self.obj[col].dtype == "O":
111107
first_val = self.obj[col].iloc[0]

pandas/io/parsers/readers.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -863,22 +863,19 @@ def read_csv(
863863

864864
def _restore_complex_arrays(df: DataFrame) -> None:
865865
"""
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.
868868
"""
869869
def looks_like_json_array(x: str) -> bool:
870870
return x.startswith("[") and x.endswith("]")
871871

872872
for col in df.columns:
873-
# Only parse object columns
874873
if df[col].dtype == "object":
875-
# skip null
876874
nonnull = df[col].dropna()
877875
if (
878876
len(nonnull) > 0
879877
and nonnull.apply(lambda x: isinstance(x, str) and looks_like_json_array(x)).all()
880878
):
881-
# parse
882879
df[col] = df[col].apply(lambda x: np.array(json.loads(x)) if pd.notnull(x) else x)
883880

884881

0 commit comments

Comments
 (0)