Output Formats
Output format handling for Quick Metric results.
Converts metric results between different output formats while preserving complex data types like DataFrames.
Classes:
| Name | Description |
|---|---|
OutputFormat : Enumeration of supported output formats |
|
Functions:
| Name | Description |
|---|---|
format_results : Convert results to specified output format |
|
to_dataframe : Convert nested results to pandas DataFrame |
|
to_records : Convert nested results to list of records |
|
to_flat_dataframe : Convert nested results to flattened DataFrame |
|
OutputFormat
Bases: Enum
Supported output formats for generated metrics.
Source code in quick_metric/_output_formats.py
convert_to_records(results)
Convert nested results to records format.
Keeps complex data types (DataFrames, Series) intact rather than flattening.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict
|
Nested results in format {'metric': {'method': result}} |
required |
Returns:
| Type | Description |
|---|---|
List[dict]
|
Records with structure [{'metric': str, 'method': str, 'value': Any}] |
Source code in quick_metric/_output_formats.py
convert_to_dataframe(results)
Convert nested results to DataFrame format.
Creates a long-format DataFrame while preserving complex data types. For DataFrames and Series, stores the object reference rather than flattening.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict
|
Nested results in format {'metric': {'method': result}} |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Long format DataFrame with columns [metric, method, value, value_type] |
Source code in quick_metric/_output_formats.py
convert_to_flat_dataframe(results)
Convert nested results to a completely flat DataFrame format.
This format creates one row per data point with clean, simple columns: - metric: metric name - method: method name - group_by: grouping variable(s) - single values or tuples for multiple groups - statistic: the statistic/column being measured - metric_value: the actual metric value
Benefits of using tuples in group_by: - Preserves grouping structure instead of concatenating strings - Allows filtering by individual grouping levels: result[result['group_by'].apply(lambda x: x[0] == 'East')] - Maintains data types instead of converting everything to strings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict
|
Nested results dictionary |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Flat DataFrame with one row per data point |
Source code in quick_metric/_output_formats.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
convert_to_format(results, output_format)
Convert nested results to the specified output format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict
|
Nested results from interpret_metric_instructions |
required |
output_format
|
OutputFormat
|
Desired output format |
required |
Returns:
| Type | Description |
|---|---|
Union[dict, DataFrame, List[dict]]
|
Results in the specified format |
Raises:
| Type | Description |
|---|---|
ValueError
|
If output_format is not supported |
Source code in quick_metric/_output_formats.py
to_dataframe(results)
Convert any results format to pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict or List[dict]
|
Results in nested or records format |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Results as DataFrame |
Source code in quick_metric/_output_formats.py
to_records(results)
Convert any results format to records format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict or DataFrame
|
Results in nested or dataframe format |
required |
Returns:
| Type | Description |
|---|---|
List[dict]
|
Results as list of dictionaries |
Source code in quick_metric/_output_formats.py
to_nested(results)
Convert results back to nested dictionary format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
DataFrame or List[dict]
|
Results in dataframe or records format |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Results in nested format {'metric': {'method': value}} |