Copilot commented on code in PR #40738: URL: https://github.com/apache/superset/pull/40738#discussion_r3364262656
########## tests/unit_tests/pandas_postprocessing/test_rank.py: ########## @@ -0,0 +1,54 @@ +# 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. +import numpy as np + +from superset.utils import pandas_postprocessing as pp +from tests.unit_tests.fixtures.dataframes import categories_df + + +def test_rank_should_rank(): + # Here we use np.isclose to avoid "false positives" in != tests + # Plain + _categories_df = categories_df.copy(deep=True) + assert np.isclose( + pp.rank(_categories_df, "asc_idx")["rank"], + np.linspace(1.0 / 101.0, 1.0, 101), + rtol=1e-8, + ).all() + + # Grouped + gb = pp.rank(_categories_df, "asc_idx", "dept").groupby("dept") + res = gb.apply( + lambda x: np.isclose( + x.sort_values("rank")["rank"], + np.linspace(1.0 / len(x), 1.0, len(x)), + rtol=1e-8, + ).all() + ) + assert res.all() + + +def test_rank_single_cat(): + # Check that reducing the category to one value still holds valid results + _categories_df = categories_df.copy(deep=True) + + # This was raising up to 6.1.0, see https://github.com/apache/superset/issues/40709 + tmp_df = _categories_df[_categories_df["dept"] == "dept0"].reset_index(drop=True) + pp.rank(tmp_df, "asc_idx", "dept") + + assert tmp_df["rank"].min() == 1.0 / len(tmp_df) + assert tmp_df["rank"].max() == 1.0 Review Comment: These assertions compare floating-point values using exact equality, but earlier in the file you intentionally use `np.isclose` to avoid brittle float comparisons. Using exact equality here can intermittently fail due to floating rounding differences across pandas/NumPy versions. -- 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]
