Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All MT "mode" values are NULL #13

Open
NickCrews opened this issue Mar 4, 2024 · 0 comments
Open

All MT "mode" values are NULL #13

NickCrews opened this issue Mar 4, 2024 · 0 comments

Comments

@NickCrews
Copy link

NickCrews commented Mar 4, 2024

is_mt = ibis.and_(
    _.state_po == "MT",
    _.year == 2022,
)


def check_mode(t):
    isnull = _.mode.isnull()
    gb = (
        t.group_by(
            [
                "state_po",
                "office",
                "year",
            ]
        )
        .agg(
            n=_.count(),
            n_null=isnull.sum(),
            n_notnull=(~isnull).sum(),
            fraction_null=isnull.mean(),
        )
        .order_by(
            _.fraction_null.desc(),
            _.state_po,
            _.office,
            _.year,
        )
    )
    gb = gb[_.n_null > 0]
    return gb


check_mode(t.filter(is_mt))
┏━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ state_po ┃ office       ┃ year  ┃ n     ┃ n_null ┃ n_notnull ┃ fraction_null ┃
┡━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ string   │ string       │ int64 │ int64 │ int64  │ int64     │ float64       │
├──────────┼──────────────┼───────┼───────┼────────┼───────────┼───────────────┤
│ MT       │ STATE HOUSE  │  2022 │  1099 │   1099 │         0 │           1.0 │
│ MT       │ STATE SENATE │  2022 │   573 │    573 │         0 │           1.0 │
│ MT       │ US HOUSE     │  2022 │  2349 │   2349 │         0 │           1.0 │
└──────────┴──────────────┴───────┴───────┴────────┴───────────┴───────────────┘

It looks like all the mode=NULL rows should actually be "TOTAL",
there is only one row per candidate per precinct. If a candidate
had more than one row per precinct, then one of them might be TOTAL,
and the rest would be something else. But that's not the case here:

mt = t.filter(is_mt)
gb = mt.group_by(
    [
        _.district,
        _.county_name,
        _.precinct,
        _.candidate,
    ]
).mutate(
    n=_.count(),
)
gb.order_by(
    _.n.desc(),
)
┏━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ year  ┃ state_po ┃ office   ┃ district ┃ magnitude ┃ county_name ┃ precinct       ┃ candidate     ┃ party_detailed ┃ party_simplified ┃ mode   ┃ votes ┃ n     ┃
┡━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ int64 │ string   │ string   │ string   │ int64     │ string      │ string         │ string        │ string         │ string           │ string │ int64 │ int64 │
├───────┼──────────┼──────────┼──────────┼───────────┼─────────────┼────────────────┼───────────────┼────────────────┼──────────────────┼────────┼───────┼───────┤
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ BEAVERHEAD  │ PRECINCT 05    │ JOHN LAMB     │ LIBERTARIAN    │ LIBERTARIAN      │ NULL   │     6 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ BEAVERHEAD  │ PRECINCT 05    │ RYAN K ZINKE  │ REPUBLICAN     │ REPUBLICAN       │ NULL   │   106 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ DEER LODGE  │ PRECINCT 1-77W │ JOHN LAMB     │ LIBERTARIAN    │ LIBERTARIAN      │ NULL   │    18 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ DEER LODGE  │ PRECINCT 1-77W │ RYAN K ZINKE  │ REPUBLICAN     │ REPUBLICAN       │ NULL   │   159 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ DEER LODGE  │ PRECINCT 3-78  │ MONICA TRANEL │ DEMOCRAT       │ DEMOCRAT         │ NULL   │    73 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ FLATHEAD    │ PRECINCT 29    │ JOHN LAMB     │ LIBERTARIAN    │ LIBERTARIAN      │ NULL   │    58 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ FLATHEAD    │ PRECINCT 29    │ RYAN K ZINKE  │ REPUBLICAN     │ REPUBLICAN       │ NULL   │  1009 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ FLATHEAD    │ PRECINCT 3     │ JOHN LAMB     │ LIBERTARIAN    │ LIBERTARIAN      │ NULL   │    34 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ FLATHEAD    │ PRECINCT 3     │ RYAN K ZINKE  │ REPUBLICAN     │ REPUBLICAN       │ NULL   │   356 │     1 │
│  2022 │ MT       │ US HOUSE │ 001      │         1 │ FLATHEAD    │ PRECINCT 36    │ MONICA TRANEL │ DEMOCRAT       │ DEMOCRAT         │ NULL   │   415 │     1 │
│     … │ …        │ …        │ …        │         … │ …           │ …              │ …             │ …              │ …                │ …      │     … │     … │
└───────┴──────────┴──────────┴──────────┴───────────┴─────────────┴────────────────┴───────────────┴────────────────┴──────────────────┴────────┴───────┴───────┘
@NickCrews NickCrews changed the title Some MT and IN "mode" values are NULL Some MT "mode" values are NULL Mar 4, 2024
@NickCrews NickCrews changed the title Some MT "mode" values are NULL All MT "mode" values are NULL Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant