Skip to content

Commit

Permalink
fix(server): correctly generate one parent and one child row even if …
Browse files Browse the repository at this point in the history
…labels are the same TCTC-8331 (#2072)

* test(server): add row when parent and child have the same label

In mongo, it generates one child and one parent row.
In pandas, it generates 2 parent rows (bug).

* fix(server): correctly generate one parent and one child dow even if labels are the same (pandas, waterfall step)

* style(server): lint

* docs(server): changelog note
  • Loading branch information
davinov authored Apr 17, 2024
1 parent 5fac132 commit 87a17ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

- Pandas: Waterfall step: generate one parent and one child row even if labels are the same (instead of two parent rows)

## [0.42.2] - 2024-03-12

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ def _merge(step: WaterfallStep, start_df: DataFrame, end_df: DataFrame) -> DataF
{RESULT_COLUMN: "sum"}
)
parents_results[step.labelsColumn] = parents_results[step.parentsColumn]
parents_results[TYPE_WATERFALL_COLUMN] = "parent"
merged_df[TYPE_WATERFALL_COLUMN] = "child"
return pd.concat([merged_df, parents_results])
else:
merged_df[TYPE_WATERFALL_COLUMN] = "parent"
return merged_df


Expand Down Expand Up @@ -207,20 +211,13 @@ def _compute_parents_and_children(step: WaterfallStep, merged_df: DataFrame) ->
"""Computes children values (the deepest nesting level)"""
result_df = DataFrame({LABEL_WATERFALL_COLUMN: merged_df[step.labelsColumn].astype(str)})
result_df[step.groupby] = merged_df[step.groupby]
result_df[TYPE_WATERFALL_COLUMN] = merged_df[TYPE_WATERFALL_COLUMN]

if step.parentsColumn is None:
result_df[TYPE_WATERFALL_COLUMN] = "parent"
result_df[_VQB_SORT_ORDER] = 2
else:
result_df[GROUP_WATERFALL_COLUMN] = merged_df[step.parentsColumn]
result_df[TYPE_WATERFALL_COLUMN] = numpy.where(
result_df[LABEL_WATERFALL_COLUMN] == (result_df[GROUP_WATERFALL_COLUMN]),
"parent",
"child",
)
result_df[_VQB_SORT_ORDER] = numpy.where(
result_df[LABEL_WATERFALL_COLUMN] == (result_df[GROUP_WATERFALL_COLUMN]), 2, 1
)
result_df[_VQB_SORT_ORDER] = numpy.where(result_df[TYPE_WATERFALL_COLUMN] == "parent", 2, 1)

result_df[step.valueColumn] = merged_df[RESULT_COLUMN]
return result_df
Expand Down
15 changes: 14 additions & 1 deletion server/tests/backends/fixtures/waterfall/with_backfill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ input:
country: Germany
month: 01 April 2023
price: 65076
# Only element with the same name as the group
- account: England
country: England
month: 01 January 2023
price: 123456
schema:
fields:
- name: account
Expand All @@ -275,7 +280,7 @@ expected:
- GROUP_waterfall: 01 January 2023
LABEL_waterfall: 01 January 2023
TYPE_waterfall: null
price: 119907
price: 243363
- GROUP_waterfall: Germany
LABEL_waterfall: Fermentum Industries
TYPE_waterfall: child
Expand All @@ -296,6 +301,10 @@ expected:
LABEL_waterfall: Dignissim Maecenas Foundation
TYPE_waterfall: child
price: -62202
- GROUP_waterfall: England
LABEL_waterfall: England
TYPE_waterfall: child
price: -123456
- GROUP_waterfall: Germany
LABEL_waterfall: Germany
TYPE_waterfall: parent
Expand All @@ -304,6 +313,10 @@ expected:
LABEL_waterfall: France
TYPE_waterfall: parent
price: -78287
- GROUP_waterfall: England
LABEL_waterfall: England
TYPE_waterfall: parent
price: -123456
- GROUP_waterfall: 01 December 2023
LABEL_waterfall: 01 December 2023
TYPE_waterfall: null
Expand Down

0 comments on commit 87a17ae

Please sign in to comment.