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

Function process_df

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

Processing steps needed before writing / after reading We only modify the dataframe to optimise size, rather than convert/infer types, e.g. no longer parsing dates from strings NOTE - this mutates the dataframe by default but returns it - use the returned copy!

(df: pd.DataFrame, copy: bool = False)

Source from the content-addressed store, hash-verified

134
135
136def process_df(df: pd.DataFrame, copy: bool = False) -> pd.DataFrame:
137 """
138 Processing steps needed before writing / after reading
139 We only modify the dataframe to optimise size,
140 rather than convert/infer types, e.g. no longer parsing dates from strings
141
142 NOTE - this mutates the dataframe by default but returns it - use the returned copy!
143 """
144 if copy:
145 df = df.copy(deep=True)
146
147 convert_axis(df)
148
149 # convert timedelta
150 timedelta_to_str(df)
151
152 # NOTE - pandas >= 1.3 handles downcasting of nullable values correctly
153 df = df.convert_dtypes()
154 downcast_numbers(df)
155
156 # save timedeltas cols (unneeded whilst timedelta_to_str used)
157 # td_col = df.select_dtypes("timedelta")
158 # df[td_col.columns] = td_col
159 obj_to_str(df)
160 parse_categories(df)
161
162 # convert all strings to use the arrow dtype
163 str_to_arrow_str(df)
164
165 return df
166
167
168def to_df(value: Any) -> pd.DataFrame:

Callers 7

check_df_equalFunction · 0.90
_test_orderFunction · 0.90
_test_dfFunction · 0.90
convert_csv_pdFunction · 0.90
convert_csv_pd_Function · 0.90
save_fileMethod · 0.85

Calls 7

convert_axisFunction · 0.85
timedelta_to_strFunction · 0.85
downcast_numbersFunction · 0.85
obj_to_strFunction · 0.85
parse_categoriesFunction · 0.85
str_to_arrow_strFunction · 0.85
copyMethod · 0.45

Tested by 5

_test_orderFunction · 0.72
_test_dfFunction · 0.72
convert_csv_pdFunction · 0.72
convert_csv_pd_Function · 0.72