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

Support as_int on bool series #2359

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ehrql/query_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ def __invert__(self: T) -> T:
"""
return _apply(qm.Function.Not, self)

@overload
def as_int(self: "PatientSeries") -> "IntPatientSeries": ...
@overload
def as_int(self: "EventSeries") -> "IntEventSeries": ...
def as_int(self):
"""
Return each value in this Boolean series as 1 (True) or 0 (False).
"""
return _apply(qm.Function.CastToInt, self)


class BoolPatientSeries(BoolFunctions, PatientSeries):
_type = bool
Expand Down
2 changes: 1 addition & 1 deletion ehrql/query_model/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class FloorDivide(Series[int]):

# Casting numeric types
class CastToInt(Series[int]):
source: Series[Numeric]
source: Series[Numeric] | Series[bool]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could save eight whole characters by writing this as:

Series[Numeric | bool]

Eight, Becky! What were you thinking?


class CastToFloat(Series[float]):
source: Series[Numeric]
Expand Down