Skip to content

Better handling of treated input in RegressionDiscontinuityBetter handling treated input #450

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions causalpy/experiments/regression_discontinuity.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@
raise DataException(
"""The treated variable should be dummy coded. Consisting of 0's and 1's only.""" # noqa: E501
)
if not self.data['treated'].dtype == 'bool':
raise ValueError("The 'treated' column must be of type bool.Please convert your data accordingly.")

Check warning on line 194 in causalpy/experiments/regression_discontinuity.py

View check run for this annotation

Codecov / codecov/patch

causalpy/experiments/regression_discontinuity.py#L194

Added line #L194 was not covered by tests

def _is_treated(self, x):
"""Returns ``True`` if `x` is greater than or equal to the treatment threshold.
Expand Down
20 changes: 20 additions & 0 deletions causalpy/experiments/test_treated_column_valid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pandas as pd
import pytest

Check warning on line 2 in causalpy/experiments/test_treated_column_valid.py

View check run for this annotation

Codecov / codecov/patch

causalpy/experiments/test_treated_column_valid.py#L1-L2

Added lines #L1 - L2 were not covered by tests

def _check_treated_column_validity(df, treated_col_name):
treated_col = df[treated_col_name]
if not pd.api.types.is_bool_dtype(treated_col):
raise ValueError(f"The '{treated_col_name}' column must be of boolean dtype (True/False).")

Check warning on line 7 in causalpy/experiments/test_treated_column_valid.py

View check run for this annotation

Codecov / codecov/patch

causalpy/experiments/test_treated_column_valid.py#L4-L7

Added lines #L4 - L7 were not covered by tests

def test_treated_column_with_integers():
df = pd.DataFrame({"treated": [0, 1, 0, 1]})
with pytest.raises(ValueError, match="treated.*must be of boolean dtype"):
_check_treated_column_validity(df, "treated")

Check warning on line 12 in causalpy/experiments/test_treated_column_valid.py

View check run for this annotation

Codecov / codecov/patch

causalpy/experiments/test_treated_column_valid.py#L9-L12

Added lines #L9 - L12 were not covered by tests

def test_treated_column_with_booleans():
df = pd.DataFrame({"treated": [True, False, True, False]})
try:
_check_treated_column_validity(df, "treated")
except ValueError:
pytest.fail("Unexpected ValueError raised")

Check warning on line 19 in causalpy/experiments/test_treated_column_valid.py

View check run for this annotation

Codecov / codecov/patch

causalpy/experiments/test_treated_column_valid.py#L14-L19

Added lines #L14 - L19 were not covered by tests