Skip to content

keyerror in groupby checking #978

Answered by cosmicBboy
stevenlis asked this question in Q&A
Discussion options

You must be logged in to vote

hi @StevenLi-DS! good question... so the Check groupby behavior is a little janky right now, there's an issue for fixing this to be more intuitive: #488.

Basically, the current behavior is the g is a dictionary mapping keys from "id" to Series objects of "sex", basically unpacking the SeriesGroupby object to {"key": pd.Series}. Not great, admittedly 😅 but #488 should make that better.

In the mean time, here's a solution:

import pandas as pd
import pandera as pa

df = pd.DataFrame({
    "id": ["A", "B", "A", "B"],
    "sex": ['F', 'M', 'F', 'M']
})

# so this implements the check that you intended
check_unique_sex = pa.Check(
    # g is a dictionary mapping keys in "id", e.g. "A", "B" to S…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@stevenlis
Comment options

@cosmicBboy
Comment options

@stevenlis
Comment options

@cosmicBboy
Comment options

Answer selected by stevenlis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants