MCPcopy Create free account
hub / github.com/datapane/datapane / obj_to_str

Function obj_to_str

python-client/src/datapane/common/df_processor.py:95–108  ·  view source on GitHub ↗

Converts remaining objects columns to str

(df: pd.DataFrame)

Source from the content-addressed store, hash-verified

93
94
95def obj_to_str(df: pd.DataFrame):
96 """Converts remaining objects columns to str"""
97 # convert objects to str / NA
98 df_str = df.select_dtypes("object")
99 df[df_str.columns] = df_str.astype("string")
100
101 # convert categorical values (as strings if object)
102 def to_str_cat_vals(x: pd.Series) -> pd.Series:
103 if x.cat.categories.dtype == np.dtype("object"):
104 x.cat.categories = x.cat.categories.astype("string")
105 return x
106
107 df_cat = df.select_dtypes("category")
108 df[df_cat.columns] = df_cat.apply(to_str_cat_vals)
109
110
111def bipartite_to_bool(df: pd.DataFrame):

Callers 3

test_to_strFunction · 0.90
load_fileMethod · 0.85
process_dfFunction · 0.85

Calls 1

applyMethod · 0.45

Tested by 1

test_to_strFunction · 0.72