dpgaspar commented on a change in pull request #9427: feat: Add post processing to QueryObject URL: https://github.com/apache/incubator-superset/pull/9427#discussion_r405394200
########## File path: tests/pandas_postprocessing_test.py ########## @@ -0,0 +1,171 @@ +# 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. +# isort:skip_file +import math +from typing import Any, List + +from pandas import Series + +from superset.utils import pandas_postprocessing as proc + +from .base_tests import SupersetTestCase +from .fixtures.dataframes import categories_df, timeseries_df + + +def series_to_list(series: Series) -> List[Any]: + """ + Converts a `Series` to a regular list, and replaces non-numeric values to + Nones. + + :param series: Series to convert + :return: list without nan or inf + """ + return [ + None + if not isinstance(val, str) and (math.isnan(val) or math.isinf(val)) + else val + for val in series.tolist() + ] + + +class PostProcessingTestCase(SupersetTestCase): + def test_aggregate_with_named_aggregators(self): + aggregates = { + "asc sum": {"column": "asc_idx", "operator": "sum"}, + "asc q2": { + "column": "asc_idx", + "operator": "percentile", + "options": {"q": 75}, + }, + "desc q1": { + "column": "desc_idx", + "operator": "percentile", + "options": {"q": 25}, + }, + } + df = proc.aggregate( + df=categories_df, groupby=["constant"], aggregates=aggregates + ) + self.assertEqual(series_to_list(df["asc sum"])[0], 5050) + self.assertEqual(series_to_list(df["asc q2"])[0], 75) + self.assertEqual(series_to_list(df["desc q1"])[0], 25) + + def test_pivot_basic_functionality(self): + aggregates = {"idx_nulls": {"operator": "sum"}} + + df = proc.pivot( + df=categories_df, + index=["name"], + columns=["category"], + aggregates=aggregates, + ) + self.assertListEqual( + df.columns.tolist(), + [("idx_nulls", "cat0"), ("idx_nulls", "cat1"), ("idx_nulls", "cat2")], + ) + self.assertEqual(len(df), 101) + self.assertEqual(df.sum()[0], 315) + + df = proc.pivot( + df=categories_df, + index=["dept"], + columns=["category"], + aggregates=aggregates, + ) + self.assertEqual(len(df), 5) + + def test_pivot_metric_fill_value(self): + aggregates = {"idx_nulls": {}} # should default to sum + df = proc.pivot( + df=categories_df, + index=["name"], + columns=["category"], + metric_fill_value=1, + aggregates=aggregates, + ) + self.assertEqual(df.sum()[0], 382) + + def test_sort(self): + df = proc.sort(df=categories_df, columns={"category": True, "asc_idx": False}) + self.assertEqual(96, series_to_list(df["asc_idx"])[1]) + + def test_rolling(self): Review comment: would be nice to have tests that test the Exception being thrown, unsupported rolling type ---------------------------------------------------------------- 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]
