flatten_levels(data, column_reducer=None, index_reducer=None, column_name=..., index_name=..., inplace=False)
Flattens the columns and/or index of a pandas DataFrame or Series using reducer functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | Series
|
DataFrame or Series to flatten its MultiIndex index/columns. |
required |
column_reducer |
None | ReducerType
|
The reducer function to apply to the columns. If None, the columns will not be flattened. Defaults to None. |
None
|
index_reducer |
None | ReducerType
|
The reducer function to apply to the index. If None, the index will not be flattened. Defaults to None. |
None
|
column_name |
str | None
|
Name of the new column. If unset, will create a name. |
...
|
index_name |
str | None
|
Name of the new index. If unset, will create a name. |
...
|
inplace |
bool
|
If True, the flattening is done in-place. If False, a copy is made. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
DataFrame | Series | None
|
pd.DataFrame | pd.Series | None: If inplace is False, returns the flattened DataFrame or Series. If inplace is True, returns None. |
Source code in wt_ml/utils/pandas_utils.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
flatten_multi_index(multi_index, reducer, name=...)
Flattens a pandas MultiIndex into a single-level Index using a reducer function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
multi_index |
MultiIndex
|
The pandas MultiIndex to flatten. |
required |
reducer |
ReducerType
|
The reducer function to apply to each row of the MultiIndex. If a string is provided, it must be a key in the REDUCER_DICT, and the corresponding function will be used as the reducer. |
required |
name |
str | None
|
Name of the new Index. If None, will create a name. Defaults to None. |
...
|
Returns:
Type | Description |
---|---|
Index
|
pd.Index: A single-level Index with flattened values. |
Source code in wt_ml/utils/pandas_utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
unflatten_index(index, splitter, names=...)
Unflattens a pandas str Index into a multi-level MultiIndex using a splitter function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
MultiIndex
|
The pandas Index to unflatten. |
required |
splitter |
SplitterType
|
The splitter function to apply to each row of the Index. If a string is provided, it must be a key in the SPLITTER_DICT, and the corresponding function will be used as the splitter. |
required |
names |
list[str] | None
|
Name of the new MultiIndex. If None, will create names. Defaults to None. |
...
|
Returns:
Type | Description |
---|---|
MultiIndex
|
pd.MultiIndex: A multi-level MultiIndex with unflattened values. |
Source code in wt_ml/utils/pandas_utils.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
unflatten_levels(data, column_splitter=None, index_splitter=None, column_names=..., index_names=..., inplace=False)
Unflatten the columns and/or index of a pandas DataFrame or Series using splitter functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | Series
|
DataFrame or Series to unflatten its MultiIndex index/columns. |
required |
column_splitter |
None | SplitterType
|
The splitter function to apply to the columns. If None, the columns will not be unflattened. Defaults to None. |
None
|
index_splitter |
None | SplitterType
|
The splitter function to apply to the index. If None, the index will not be unflattened. Defaults to None. |
None
|
column_names |
list[str] | None
|
New column names. If unset, will create names. |
...
|
index_names |
list[str] | None
|
New index names. If unset, will create names. |
...
|
inplace |
bool
|
If True, the unflattening is done in-place. If False, a copy is made. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
DataFrame | Series | None
|
pd.DataFrame | pd.Series | None: If inplace is False, returns the unflattened DataFrame or Series. If inplace is True, returns None. |
Source code in wt_ml/utils/pandas_utils.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|