Repository: pandas-dev/pandas
File: pandas/tests/groupby/aggregate/test_aggregate.py (Python)

Code before:
        with pytest.raises(TypeError, match=match):
            grouped.agg()

        with pytest.raises(TypeError, match=match):
            grouped.agg(a=("B", "max"), b=(1, 2, 3))

    def test_missing_raises(self):
        df = DataFrame({"A": [0, 1], "B": [1, 2]})
        msg = r"Label\(s\) \['C'\] do not exist"
        with pytest.raises(KeyError, match=msg):
            df.groupby("A").agg(c=("C", "sum"))

    def test_agg_namedtuple(self):
        df = DataFrame({"A": [0, 1], "B": [1, 2]})
        result = df.groupby("A").agg(
            b=pd.NamedAgg("B", "sum"), c=pd.NamedAgg(column="B", aggfunc="count")
        )
        expected = df.groupby("A").agg(b=("B", "sum"), c=("B", "count"))
        tm.assert_frame_equal(result, expected)

    def n_between(self, ser, low, high, **kwargs):
        return ser.between(low, high, **kwargs).sum()

    def test_namedagg_args(self):
        df = DataFrame({"A": [0, 0, 1, 1], "B": [-1, 0, 1, 2]})

        result = df.groupby("A").agg(
            count_between=pd.NamedAgg("B", self.n_between, 0, 1)
        )
        expected = DataFrame({"count_between": [1, 1]}, index=Index([0, 1], name="A"))
        tm.assert_frame_equal(result, expected)

    def test_namedagg_kwargs(self):
        df = DataFrame({"A": [0, 0, 1, 1], "B": [-1, 0, 1, 2]})

        result = df.groupby("A").agg(
            count_between_kw=pd.NamedAgg("B", self.n_between, 0, 1, inclusive="both")
        )
        expected = DataFrame(
            {"count_between_kw": [1, 1]}, index=Index([0, 1], name="A")
        )
        tm.assert_frame_equal(result, expected)

    def test_namedagg_args_and_kwargs(self):
        df = DataFrame({"A": [0, 0, 1, 1], "B": [-1, 0, 1, 2]})

        result = df.groupby("A").agg(
            count_between_mix=pd.NamedAgg(
                "B", self.n_between, 0, 1, inclusive="neither"
            )
        )

Review comment (suggestion):
Can you add `# https://github.com/pandas-dev/pandas/issues/58283` as the first line in each of these tests.

Code after:
        with pytest.raises(TypeError, match=match):
            grouped.agg()

        with pytest.raises(TypeError, match=match):
            grouped.agg(a=("B", "max"), b=(1, 2, 3))

    def test_missing_raises(self):
        df = DataFrame({"A": [0, 1], "B": [1, 2]})
        msg = r"Label\(s\) \['C'\] do not exist"
        with pytest.raises(KeyError, match=msg):
            df.groupby("A").agg(c=("C", "sum"))

    def test_agg_namedtuple(self):
        df = DataFrame({"A": [0, 1], "B": [1, 2]})
        result = df.groupby("A").agg(
            b=pd.NamedAgg("B", "sum"), c=pd.NamedAgg(column="B", aggfunc="count")
        )
        expected = df.groupby("A").agg(b=("B", "sum"), c=("B", "count"))
        tm.assert_frame_equal(result, expected)

    def n_between(self, ser, low, high, **kwargs):
        return ser.between(low, high, **kwargs).sum()

    def test_namedagg_args(self):
        # https://github.com/pandas-dev/pandas/issues/58283
        df = DataFrame({"A": [0, 0, 1, 1], "B": [-1, 0, 1, 2]})

        result = df.groupby("A").agg(
            count_between=pd.NamedAgg("B", self.n_between, 0, 1)
        )
        expected = DataFrame({"count_between": [1, 1]}, index=Index([0, 1], name="A"))
        tm.assert_frame_equal(result, expected)

    def test_namedagg_kwargs(self):
        # https://github.com/pandas-dev/pandas/issues/58283
        df = DataFrame({"A": [0, 0, 1, 1], "B": [-1, 0, 1, 2]})

        result = df.groupby("A").agg(
            count_between_kw=pd.NamedAgg("B", self.n_between, 0, 1, inclusive="both")
        )
        expected = DataFrame(
            {"count_between_kw": [1, 1]}, index=Index([0, 1], name="A")
        )
        tm.assert_frame_equal(result, expected)

    def test_namedagg_args_and_kwargs(self):
        # https://github.com/pandas-dev/pandas/issues/58283
        df = DataFrame({"A": [0, 0, 1, 1], "B": [-1, 0, 1, 2]})

        result = df.groupby("A").agg(
            count_between_mix=pd.NamedAgg(