Fix test_vasopressor_units crashing on Series.contains#1984
Open
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Open
Fix test_vasopressor_units crashing on Series.contains#1984Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Conversation
\`test_vasopressor_units\` in \`mimic-iv/tests/test_medication.py\`
checks whether any vasopressor row with a non-standard \`rateuom\`
falls outside a whitelist of known-inspected \`hadm_id\`s, and
raises a warning if so. The membership test is currently written as
if (~df['hadm_id'].contains(hadm_id_list)).any():
but \`pandas.Series\` has no \`.contains()\` method — \`.contains()\`
only lives on the \`.str\` accessor (for substring search against a
single pattern, not membership in a list). Whenever the preceding
\`gbq.read_gbq\` query returns any rows, this line raises
\`AttributeError: 'Series' object has no attribute 'contains'\`
and the test fails before it gets to the actual assertion on the
following line.
The correct pandas idiom for "row's hadm_id is in this list" is
\`Series.isin(list)\`, which is what this code needs. Replace
\`.contains(hadm_id_list)\` with \`.isin(hadm_id_list)\`; the outer
\`~ ... .any()\` still expresses "any row outside the whitelist",
matching the comment's stated intent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
`test_vasopressor_units` in `mimic-iv/tests/test_medication.py` crashes with `AttributeError` whenever the preceding `gbq.read_gbq` query returns any rows, before ever reaching its real assertion:
```
AttributeError: 'Series' object has no attribute 'contains'
```
Root cause
The test pulls rows with a non-standard `rateuom` for each vasopressor and then checks whether any row's `hadm_id` falls outside a whitelist of known-inspected `hadm_id`s:
```python
df = gbq.read_gbq(query, project_id=project_id, dialect="standard")
if we find new uninspected rows, raise a warning. this will only happen when mimic-iv is updated.
if (~df['hadm_id'].contains(hadm_id_list)).any():
_LOGGER.warn(...)
```
`pandas.Series` has no `.contains()` method — `contains` only lives on the `.str` accessor and does substring-matching against a single pattern, not membership-testing against a list. The correct pandas idiom for "row's value is in this list" is `Series.isin(...)`, which is clearly what the surrounding code intends (
hadm_id_listis a plain list of integers, not a regex).As written, the moment the query returns any row the test hits the `.contains(hadm_id_list)` call, raises `AttributeError`, and fails. The hit path is what the test is supposed to log a warning for, so the bug masks the whole reason this check exists.
Fix
Replace `.contains(hadm_id_list)` with `.isin(hadm_id_list)`:
```diff
```
The outer `~...any()` still expresses "any row falls outside the whitelist", matching the comment and the downstream warning log.