opensafely / opensafely/WinterPressuresDescriptive

Debug TypeError in ethnicity measures for post-COVID cohorts

Open
#11 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Thanks to @iaindillingham for helping to identify the issue https://github.com/opensafely-core/ehrql/issues/2445 and issue addressed by the PR: https://github.com/opensafely/WinterPressuresDescriptive/pull/10

The error was due to how the variable exp_bin_eth_missing was defined. Specifically, it used .is_null() on a Series derived from .to_category():

exp_bin_eth_missing = tmp_exp_cat_ethnicity.is_null()

However, .to_category() returns a Series that wraps a Case, and by default assigns None to unmatched codes. Unfortunately, .is_null() does not correctly detect these None values, so the resulting Boolean Series included unexpected Nones. This caused ehrQL to raise a TypeError when trying to count True values in the measure numerator.

The fix was to explicitly specify a default value in .to_category():

.to_category(ethnicity_snomed, default="Missing")

This ensures that unmatched codes are mapped to a string value ("Missing") rather than None, allowing missing ethnicity to be safely identified using:

exp_bin_eth_missing = tmp_exp_cat_ethnicity == "Missing"

This resolved the issue by producing a clean Boolean Series with only True/False values, avoiding the use of None in downstream measure calculations.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review ehrql issue 2445 and WinterPressuresDescriptive PR 10 first, then trace the ethnicity measure around exp_bin_eth_missing and tmp_exp_cat_ethnicity. The issue is resolved when the measure no longer raises a TypeError while counting missing-ethnicity values and produces only boolean values for that condition.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.