zhaoyongjie commented on a change in pull request #16945:
URL: https://github.com/apache/superset/pull/16945#discussion_r722897537
##########
File path: superset/utils/pandas_postprocessing.py
##########
@@ -535,17 +562,32 @@ def cum(df: DataFrame, columns: Dict[str, str], operator:
str) -> DataFrame:
`y2` based on cumulative values calculated from `y`, leaving the
original
column `y` unchanged.
:param operator: cumulative operator, e.g. `sum`, `prod`, `min`, `max`
+ :param is_pivot_df: Dataframe is pivoted or not
:return: DataFrame with cumulated columns
"""
- df_cum = df[columns.keys()]
+ columns = columns or {}
+ if is_pivot_df:
+ df_cum = df
+ else:
+ df_cum = df[columns.keys()]
operation = "cum" + operator
if operation not in ALLOWLIST_CUMULATIVE_FUNCTIONS or not hasattr(
df_cum, operation
):
raise QueryObjectValidationError(
_("Invalid cumulative operator: %(operator)s", operator=operator)
)
- return _append_columns(df, getattr(df_cum, operation)(), columns)
+ if is_pivot_df:
+ df_cum = getattr(df_cum, operation)()
+ agg_in_pivot_df =
df.columns.get_level_values(0).drop_duplicates().to_list()
+ agg: Dict[str, Dict[str, Any]] = {col: {} for col in agg_in_pivot_df}
+ df_cum.columns = [
+ _flatten_column_after_pivot(col, agg) for col in df_cum.columns
+ ]
Review comment:
I will create a separate PR for refactoring `_flatten_column_after_pivot`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]