Handle pandas categorical types for categorical columns in _causal_analysis.py#602
Merged
kbattocchi merged 6 commits intopy-why:mainfrom Jun 13, 2022
Merged
Handle pandas categorical types for categorical columns in _causal_analysis.py#602kbattocchi merged 6 commits intopy-why:mainfrom
kbattocchi merged 6 commits intopy-why:mainfrom
Conversation
…alysis.py
If the categorical type is set for a treatment column explicitly then there is a failure in `CausalAnalysis` class.
```
~\AppData\Local\Continuum\miniconda3\envs\nhs-hips\lib\site-packages\econml\solutions\causal_analysis\_causal_analysis.py in individualized_policy(self, Xtest, feature_index, n_rows, treatment_costs, alpha)
1714 all_costs = np.array([0] + [treatment_costs] * (len(treatment_arr) - 1))
1715 # construct index of current treatment
-> 1716 current_ind = (current_treatment.reshape(-1, 1) ==
1717 treatment_arr.reshape(1, -1)) @ np.arange(len(treatment_arr))
1718 current_cost = all_costs[current_ind]
~\AppData\Local\Continuum\miniconda3\envs\nhs-hips\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
67 other = item_from_zerodim(other)
68
---> 69 return method(self, other)
70
71 return new_method
~\AppData\Local\Continuum\miniconda3\envs\nhs-hips\lib\site-packages\pandas\core\arrays\categorical.py in func(self, other)
131 if is_list_like(other) and len(other) != len(self) and not hashable:
132 # in hashable case we may have a tuple that is itself a category
--> 133 raise ValueError("Lengths must match.")
134
135 if not self.ordered:
```
Solution is to check for the type of the categorical column to see if it is of type `pd.core.arrays.categorical.Categorical` and extract the numpy array using `to_numpy()` method.
Member
|
Please add a test that only passes with the new code. |
kbattocchi
requested changes
Apr 12, 2022
Member
kbattocchi
left a comment
There was a problem hiding this comment.
Please add a test that ensures that we don't regress this.
kbattocchi
approved these changes
Jun 13, 2022
Member
kbattocchi
left a comment
There was a problem hiding this comment.
I've added a test that verifies that the change fixes the behavior.
3 tasks
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.
If the categorical type is set for a treatment column explicitly then there is a failure in
CausalAnalysisclass.Solution is to check for the type of the categorical column to see if it is of type
pd.core.arrays.categorical.Categoricaland extract the numpy array usingto_numpy()method.