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

Function convert_axis

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

flatten both columns and indexes

(df: pd.DataFrame)

Source from the content-addressed store, hash-verified

12
13
14def 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
34def downcast_numbers(data: pd.DataFrame):

Callers 2

_test_dfFunction · 0.90
process_dfFunction · 0.85

Calls

no outgoing calls

Tested by 1

_test_dfFunction · 0.72