Skip to content

Commit 371ce50

Browse files
authored
Merge pull request #1128 from Martozar/c.mze-cq-1474
feat: add column total header indexes to DataFrameMetadata
2 parents af45dfc + 5c3a7f4 commit 371ce50

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

gooddata-pandas/gooddata_pandas/result_convertor.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,13 @@ class DataFrameMetadata:
437437
| AVG | 150
438438
SUM | | 450
439439
440+
column_totals_indexes: Similar to row_totals_indexes but for column headers.
440441
execution_response: An instance of BareExecutionResponse representing the
441442
execution response.
442443
"""
443444

444445
row_totals_indexes: list[list[int]]
446+
column_totals_indexes: list[list[int]]
445447
execution_response: BareExecutionResponse
446448
primary_labels_from_index: dict[int, dict[str, str]]
447449
primary_labels_from_columns: dict[int, dict[str, str]]
@@ -460,17 +462,25 @@ def from_data(
460462
A tuple containing data headers. execution_response (BareExecutionResponse): An ExecutionResponse object.
461463
462464
Returns: DataFrameMetadata: An initialized DataFrameMetadata object."""
463-
row_totals_indexes = [
464-
[idx for idx, hdr in enumerate(dim) if hdr is not None and hdr.get("totalHeader") is not None]
465-
for dim in headers[0]
466-
]
465+
row_totals_indexes = cls._get_totals_indexes(headers[0])
466+
column_totals_indexes = cls._get_totals_indexes(headers[1])
467467
return cls(
468468
row_totals_indexes=row_totals_indexes,
469+
column_totals_indexes=column_totals_indexes,
469470
execution_response=execution_response,
470471
primary_labels_from_index=primary_labels_from_index,
471472
primary_labels_from_columns=primary_labels_from_columns,
472473
)
473474

475+
@staticmethod
476+
def _get_totals_indexes(headers: Optional[Any]) -> list[list[int]]:
477+
if headers is None:
478+
return []
479+
return [
480+
[idx for idx, hdr in enumerate(dim) if hdr is not None and hdr.get("totalHeader") is not None]
481+
for dim in headers
482+
]
483+
474484

475485
def _read_complete_execution_result(
476486
execution_response: BareExecutionResponse,

gooddata-pandas/tests/dataframe/test_dataframe_for_exec_def.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def _run_and_validate_results(
2828
exec_def: ExecutionDefinition,
2929
expected: tuple[int, int],
3030
expected_row_totals: Optional[list[list[int]]] = None,
31+
expected_column_totals: Optional[list[list[int]]] = None,
3132
page_size: int = 100,
3233
optimized: bool = False,
3334
) -> str:
@@ -42,6 +43,8 @@ def _run_and_validate_results(
4243

4344
if expected_row_totals is not None:
4445
assert result_metadata_from_result_id.row_totals_indexes == expected_row_totals
46+
if expected_column_totals is not None:
47+
assert result_metadata_from_result_id.column_totals_indexes == expected_column_totals
4548

4649
assert result_from_result_id.values.shape == expected
4750

@@ -349,7 +352,9 @@ def test_dataframe_for_exec_def_totals4(gdf: DataFrameFactory, optimized: bool):
349352
),
350353
],
351354
)
352-
_run_and_validate_results(gdf=gdf, exec_def=exec_def, expected=(96, 19), optimized=optimized)
355+
_run_and_validate_results(
356+
gdf=gdf, exec_def=exec_def, expected=(96, 19), optimized=optimized, expected_column_totals=[[17, 18], [17, 18]]
357+
)
353358

354359

355360
# TODO - not implemented yet

0 commit comments

Comments
 (0)