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)
| 134 | |
| 135 | |
| 136 | def 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 | |
| 168 | def to_df(value: Any) -> pd.DataFrame: |