villebro commented on a change in pull request #10286:
URL:
https://github.com/apache/incubator-superset/pull/10286#discussion_r452949522
##########
File path: superset/utils/pandas_postprocessing.py
##########
@@ -517,3 +518,29 @@ def _parse_location(location: str) -> Tuple[float, float,
float]:
return _append_columns(df, geodetic_df, columns)
except ValueError:
raise QueryObjectValidationError(_("Invalid geodetic string"))
+
+
+def contribution(
+ df: DataFrame, orientation: PostProcessingContributionOrientation
+) -> DataFrame:
+ """
+ Calculate cell contibution to row/column total.
+
+ :param df: DataFrame containing all-numeric data (temporal column ignored)
+ :param orientation: calculate by dividing cell with row/column total
+ :return: DataFrame with contributions, with temporal column at beginning
if present
+ """
+ temporal_series: Optional[Series] = None
+ contribution_df = df.copy()
+ if DTTM_ALIAS in df.columns:
+ temporal_series = cast(Series, contribution_df.pop(DTTM_ALIAS))
+
+ if orientation == PostProcessingContributionOrientation.ROW:
+ contribution_dft = contribution_df.T
+ contribution_df = (contribution_dft / contribution_dft.sum()).T
+ else:
+ contribution_df = contribution_df / contribution_df.sum()
Review comment:
I was just lazy and more or less copied what was in `viz.py` 😛
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]