ktmud commented on a change in pull request #9427: feat: Add post processing to QueryObject URL: https://github.com/apache/incubator-superset/pull/9427#discussion_r405860350
########## File path: superset/utils/pandas_postprocessing.py ########## @@ -0,0 +1,323 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from functools import partial +from typing import Any, Dict, KeysView, List, Optional, Sequence, Union + +import numpy as np +from flask_babel import gettext as _ +from pandas import DataFrame, NamedAgg + +from superset.exceptions import ChartDataValidationError + +SUPPORTED_NUMPY_FUNCTIONS = ( + "average", + "argmin", + "argmax", + "cumsum", + "cumprod", + "max", + "mean", + "median", + "nansum" "nanmin" "nanmax" "nanmean", + "nanmedian", + "min", + "percentile", + "prod", + "product", + "std", + "sum", + "var", +) + + +def _validate_columns( + df: DataFrame, columns: Union[KeysView[str], Sequence[str]] +) -> None: + """ + Ensure that all columns are present in the DataFrame + + :param df: Base DataFrame + :param columns: column existence to check in `df` + """ + if not all(elem in df.columns.tolist() for elem in columns): + raise ChartDataValidationError( + _( + "Referenced columns not available in DataFrame. " + "Columns in DataFrame: %(df_cols)s. Referenced columns: %(columns)s", + df_cols=list(df.columns), + columns=list(columns), + ) + ) + + +def _get_aggregate_funcs(aggregates: Dict[str, Dict[str, Any]],) -> Dict[str, NamedAgg]: + """ + Converts a set of aggregate config objects into functions that pandas can use as + aggregators. Currently only numpy aggregators are supported. + + :param aggregates: Mapping from column name to aggregat config. + :return: Mapping from metric name to function that takes a single input argument. + """ + agg_funcs: Dict[str, NamedAgg] = {} + for name, agg_obj in aggregates.items(): + column = agg_obj.get("column", name) + operator = agg_obj.get("operator") or "sum" Review comment: `agg_obj.get("operator", "sum")` is probably better because the user shouldn't pass an empty string as the `operator`, while omitting is fine. ---------------------------------------------------------------- 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] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
