flatten both columns and indexes
(df: pd.DataFrame)
| 12 | |
| 13 | |
| 14 | def convert_axis(df: pd.DataFrame): |
| 15 | """flatten both columns and indexes""" |
| 16 | |
| 17 | # flatten hierarchical columns and convert to strings |
| 18 | if df.columns.nlevels > 1: |
| 19 | df.columns = ["/".join(a) for a in df.columns.to_flat_index()] |
| 20 | |
| 21 | df.columns = df.columns.astype("string") |
| 22 | |
| 23 | # flatten/reset indexes |
| 24 | if isinstance(df.index, pd.RangeIndex): |
| 25 | pass # allow RangeIndex - reset numbers? |
| 26 | elif isinstance(df.index, pd.Int64Index): |
| 27 | df.reset_index(inplace=True, drop=True) |
| 28 | else: |
| 29 | # reset if any other index type, e.g. MultiIndex, custom Index |
| 30 | # all new column dtypes will converted in latter functions |
| 31 | df.reset_index(inplace=True) |
| 32 | |
| 33 | |
| 34 | def downcast_numbers(data: pd.DataFrame): |
no outgoing calls