Skip to content

Commit e831937

Browse files
authored
Merge pull request #54 from histogrammar/perf_casting_object_datatype
pd object series cast to string
2 parents 8f8275f + 64a5a3e commit e831937

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

histogrammar/dfinterface/filling_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def to_str(val):
8383
"""
8484
if isinstance(val, str):
8585
return val
86+
elif isinstance(val, pd.Series):
87+
# Note: at this point, data type of pd.series has already been inferred as being of type object (mixed)
88+
return val.astype(str).values
8689
elif hasattr(val, "__iter__"):
8790
return np.asarray(
8891
list(
@@ -110,9 +113,10 @@ def only_str(val):
110113
"""
111114
if isinstance(val, str):
112115
return val
113-
elif isinstance(val, pd.Series) and val.dtype in [str, object]:
114-
# SB 18052022: object may contain non-str
115-
return val.values
116+
elif isinstance(val, pd.Series):
117+
# at this point, data type of pd.series has already been inferred as *to be* 'string'
118+
dtype = np.dtype(val.dtype).type
119+
return val.values if dtype in [str, np.str_, np.string_] else val.astype(str).values
116120
elif hasattr(val, "__iter__"):
117121
return np.asarray([s if isinstance(s, str) else "None" for s in val])
118122
return "None"

0 commit comments

Comments
 (0)