Repository: alteryx/woodwork
Pull request #1612 (2023-01-09 16:39:14)

Update `Boolean` `transform` to avoid running a check for nans if the `dtype == bool`

codecov[bot]: # [Codecov](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx) Report
> Merging [#1612](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx) (33c6086) into [main](https://codecov.io/gh/alteryx/woodwork/commit/35c87c69625c30e49950ebc7f5b3af2198e816c9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx) (35c87c6) will **decrease** coverage by `81.20%`.
> The diff coverage is `0.00%`.

```diff
@@             Coverage Diff             @@
##             main    #1612       +/-   ##
===========================================
- Coverage   98.77%   17.57%   -81.21%     
===========================================
  Files          98       98               
  Lines       11573    11575        +2     
===========================================
- Hits        11431     2034     -9397     
- Misses        142     9541     +9399     
```


| [Impacted Files](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx) | Coverage Δ | |
|---|---|---|
| [woodwork/logical\_types.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvbG9naWNhbF90eXBlcy5weQ==) | `44.93% <0.00%> (-55.07%)` | :arrow_down: |
| [woodwork/tests/test\_config.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvdGVzdF9jb25maWcucHk=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/test\_version.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvdGVzdF92ZXJzaW9uLnB5) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/utils/test\_utils.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvdXRpbHMvdGVzdF91dGlscy5weQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/utils/test\_concat.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvdXRpbHMvdGVzdF9jb25jYXQucHk=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/type\_system/conftest.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvdHlwZV9zeXN0ZW0vY29uZnRlc3QucHk=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/utils/test\_read\_file.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvdXRpbHMvdGVzdF9yZWFkX2ZpbGUucHk=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/accessor/test\_indexers.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvYWNjZXNzb3IvdGVzdF9pbmRleGVycy5weQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/demo\_tests/test\_retail.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvZGVtb190ZXN0cy90ZXN0X3JldGFpbC5weQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [woodwork/tests/fixtures/datetime\_freq.py](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx#diff-d29vZHdvcmsvdGVzdHMvZml4dHVyZXMvZGF0ZXRpbWVfZnJlcS5weQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| ... and [60 more](https://codecov.io/gh/alteryx/woodwork/pull/1612?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx) | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alteryx)

Cmancuso: Do we need to test that Value Error? Otherwise lgtm
bchen1116: Might be useful for us to run our own LG tests to make sure this is ok!
ParthivNaresh: @Cmancuso Added a test just to be sure!

Patch:
diff --git a/docs/source/release_notes.rst b/docs/source/release_notes.rst
index d1d5918..a076ddb 100644
--- a/docs/source/release_notes.rst
+++ b/docs/source/release_notes.rst
@@ -9,6 +9,7 @@ Future Release
     * Fixes
     * Changes
         * Bump scipy and scikit-learn min versions for compatibility with numpy 1.24.0 (:pr:`1606`)
+        * Changed the ``transform`` function for the ``Boolean`` logical type to improve runtime (:pr:`1612`)
     * Documentation Changes
     * Testing Changes
 
diff --git a/woodwork/logical_types.py b/woodwork/logical_types.py
index c9c75ef..5b09974 100644
--- a/woodwork/logical_types.py
+++ b/woodwork/logical_types.py
@@ -223,16 +223,18 @@ class Boolean(LogicalType):
         Returns:
             Series: Returns column transformed into boolean type
         """
+        ve = ValueError(
+            "Expected no null values in this Boolean column. If you want to keep the nulls, use BooleanNullable type. Otherwise, cast these nulls to a boolean value with the `cast_null_as` parameter.",
+        )
         is_dask = _is_dask_series(series)
-        if (is_dask and series.isna().any().compute()) or (
-            not is_dask and series.isna().any()
-        ):
-            if self.cast_nulls_as is None:
-                raise ValueError(
-                    "Expected no null values in this Boolean column. If you want to keep the nulls, use BooleanNullable type. Otherwise, cast these nulls to a boolean value with the `cast_null_as` parameter.",
-                )
-            series.fillna(self.cast_nulls_as, inplace=True)
-        series = _coerce_boolean(series, True)
+        if not pdtypes.is_dtype_equal("bool", series.dtype):
+            if (is_dask and series.isna().any().compute()) or (
+                not is_dask and series.isna().any()
+            ):
+                if self.cast_nulls_as is None:
+                    raise ve
+                series.fillna(self.cast_nulls_as, inplace=True)
+            series = _coerce_boolean(series, True)
         return super().transform(series)
 
 


Test patch:
diff --git a/woodwork/tests/logical_types/test_logical_types.py b/woodwork/tests/logical_types/test_logical_types.py
index 7f17a15..09842a9 100644
--- a/woodwork/tests/logical_types/test_logical_types.py
+++ b/woodwork/tests/logical_types/test_logical_types.py
@@ -1,5 +1,6 @@
 import re
 from datetime import datetime
+from unittest.mock import patch
 
 import numpy as np
 import pandas as pd
@@ -909,7 +910,7 @@ def test_datetime_pivot_point_no_format_provided():
         "01/01/52",
         "01/01/56",
         "01/01/60",
-        "01/01/72",
+        "01/01/73",
         "01/01/76",
         "01/01/80",
         None,
@@ -923,7 +924,7 @@ def test_datetime_pivot_point_no_format_provided():
         "2052-01-01",
         "2056-01-01",
         "2060-01-01",
-        "1972-01-01",
+        "1973-01-01",
         "1976-01-01",
         "1980-01-01",
         None,
@@ -1109,3 +1110,11 @@ def test_boolean_strings_of_numeric_work():
     pd.testing.assert_frame_equal(pd.DataFrame.from_dict(expected), df)
     config_len_after = len(config.get_option("boolean_inference_strings"))
     assert config_len == config_len_after
+
+
+@patch("woodwork.logical_types._coerce_boolean")
+def test_coerce_boolean_not_called_for_bool_dtype(coerce_boolean_patch):
+    series = pd.Series([0, 1, 1, 0, 1, 0, 1], dtype="bool")
+    series_init = init_series(series)
+    assert not coerce_boolean_patch.called
+    assert series_init.dtype == "bool"