BUG: fixed .str.contains(..., na=False) for categorical series
State: closed

- [x] closes #22158
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Missing-data
description: np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
name: Strings
description: String extension data type and string data
name: Categorical
description: Categorical Data Type

Comments:
Failure is at https://travis-ci.org/pandas-dev/pandas/jobs/411345500#L2287-L2289

Basically, you're fix only works if there *are* missing values.

This may fix it, but may break other things

```diff
diff --git a/pandas/core/strings.py b/pandas/core/strings.py
index 8097ed196..06fd1391e 100644
--- a/pandas/core/strings.py
+++ b/pandas/core/strings.py
@@ -1855,7 +1855,12 @@ class StringMethods(NoNewAttributesMixin):
         # before the transformation...
         if use_codes and self._is_categorical:
             result = take_1d(result, self._orig.cat.codes)
-            result[isna(result)] = na
+            missing = isna(result)
+
+            if missing.any():
+                result_type = np.result_type(result, na)
+                result = result.astype(result_type, copy=False)
+                result[isna(result)] = na
 
         if not hasattr(result, 'ndim') or not hasattr(result, 'dtype'):
             return result
```
Hello @pulkitmaloo! Thanks for updating the PR.

 - There are no PEP8 issues in the file [`pandas/core/strings.py`](https://github.com/pulkitmaloo/pandas/blob/3408920da4f0db3de9ca025742a3d739dd61dd1f/pandas/core/strings.py) !

 - There are no PEP8 issues in the file [`pandas/tests/test_strings.py`](https://github.com/pulkitmaloo/pandas/blob/3408920da4f0db3de9ca025742a3d739dd61dd1f/pandas/tests/test_strings.py) !



##### Comment last updated on September 24, 2018 at 01:15 Hours UTC
Looks like a maybe unrelated CI failure. Can you merge master and push again?
And can you ensure (and test) that `na` is handled correctly for all the string functions? It looks like at least `match` isn't handled correctly.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22170?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@960a73f`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/22170/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/22170?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master   #22170   +/-   ##
=========================================
  Coverage          ?   92.19%           
=========================================
  Files             ?      169           
  Lines             ?    50950           
  Branches          ?        0           
=========================================
  Hits              ?    46975           
  Misses            ?     3975           
  Partials          ?        0
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.62% <100%> (?)` | |
| #single | `42.27% <0%> (?)` | |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/22170?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/strings.py](https://codecov.io/gh/pandas-dev/pandas/pull/22170/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc3RyaW5ncy5weQ==) | `98.58% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22170?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22170?src=pr&el=footer). Last update [960a73f...44b36a4](https://codecov.io/gh/pandas-dev/pandas/pull/22170?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@jreback Thanks for the feedback, I have updated the PR as you requested. However, the changes in yaml files leads to circleci failure.
@pulkitmaloo you need to rebase on master
@pulkitmaloo are you able to update?
@TomAugspurger should be fixed up.
thanks @pulkitmaloo