From 4fad0d6d00fd21370c72e8561646bb0370da463d Mon Sep 17 00:00:00 2001 From: "samuel.oranyeli" Date: Wed, 1 Feb 2023 13:50:00 +1100 Subject: [PATCH] add more tests --- janitor/functions/summarize.py | 2 +- tests/functions/test_summarize.py | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/janitor/functions/summarize.py b/janitor/functions/summarize.py index 1a4995e65..77a39c363 100644 --- a/janitor/functions/summarize.py +++ b/janitor/functions/summarize.py @@ -118,7 +118,7 @@ def summarize( grp = df.groupby(**by) elif by_is_true and isinstance(by, col): if by.func: - raise ValueError("Function assignment is not required within by.") + raise ValueError("Function assignment is not required within by") cols = get_index_labels([*by.cols], df, axis="columns") if by.remove_cols: exclude = get_index_labels([*by.remove_cols], df, axis="columns") diff --git a/tests/functions/test_summarize.py b/tests/functions/test_summarize.py index f212af6cb..b78cce7bb 100644 --- a/tests/functions/test_summarize.py +++ b/tests/functions/test_summarize.py @@ -3,7 +3,7 @@ import pandas as pd from pandas.testing import assert_frame_equal -from pandas.api.types import is_numeric_dtype +from pandas.api.types import is_numeric_dtype, is_string_dtype from janitor import col @@ -19,6 +19,20 @@ def test_Column_agg_error(dataframe): dataframe.summarize(col("a").compute(func)) +@pytest.mark.functions +def test_Column_by_error(dataframe): + """ + Raise if `by` is a col class, and has a function + assigned to it + """ + with pytest.raises( + ValueError, match="Function assignment is not required within by" + ): + dataframe.summarize( + col("a").compute("sum"), by=col("b").compute("size") + ) + + @pytest.mark.functions def test_Column_exclude_error(dataframe): """ @@ -210,5 +224,7 @@ def test_dataframe(): df = pd.DataFrame(df) expected = df.groupby("A").C.describe().add_prefix("C_") - actual = df.summarize(col("C").compute("describe"), by="A") + actual = df.summarize( + col("C").compute("describe"), by=col(is_string_dtype).exclude("B") + ) assert_frame_equal(expected, actual)