PolicyEngine / policyengine-core

Core microsimulation engine for PolicyEngine models. Forked from OpenFisca-Core.
https://policyengine.github.io/policyengine-core
GNU Affero General Public License v3.0
14 stars 19 forks source link

Replace numpy-Python comparison with dtype #210

Closed anth-volk closed 4 months ago

anth-volk commented 4 months ago

What's changed

Fixes #100. Previously, in #205, we downgraded one line of enums.py. The reason for this line was previously unclear, but notably, it always runs: it takes the array variable, determines whether it equals 0 (which yields True or False), then determines whether the resulting value is a Boolean, which it always is.

This was construed as a means of checking whether or not an array is of Boolean type in the comments, but what this piece of code actually does is converts all arrays passed into the function to Numpy's Unicode-string type. However, that is ambiguous due to how it's written, as well as Numpy's own confusing syntax. I believe this code also raises a FutureWarning because it takes a Numpy ndarray type and compares it with a Python type, in this case, a number.

In order to silence the warning and clean up the code, this PR instead determines whether the inbound array is of dtype "S," which confusingly stands for "bytewise-string." If so, it converts it to "str" type, which yields a dtype "U," or "Unicode-string," array.

Tests

This was tested via a Jupyter notebook with a sample policyengine-uk setup that mirrors Nikhil's in #203. That said, it has not been tested with policyengine-us.

anth-volk commented 4 months ago

Test has been added