Skip to content

pa.check with groupby argument throws TypeError #1235

Description

@Shelelena

Describe the bug
After upgrading to the latest pandera I've encountered a TypeError: object of type 'int' has no len() in a check that uses groupby argument.

Code Sample, a copy-pastable example

import pandas as pd
from pandera import DataFrameModel, check
from pandera.typing import Series


class Model(DataFrameModel):
    x: Series[int]
    y: Series[int]

    @check('x', groupby='y')
    def my_check(cls, groups):
        return True


df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})

Model.validate(df)

Suggestions

This line of code throws the exception. I would suggest changing

{
    (k if isinstance(k, bool) else k[0] if len(k) == 1 else k): v
    for k, v in groupby_obj
}

to something like

{
    (k if np.isscalar(k) or len(k) != 1 else k[0]): v
    for k, v in groupby_obj
}

Versions

python 3.11.2
pandas 2.0.2
pandera 0.15.1

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions