BUG: Copy categorical codes if empty (fixes #18051)
State: closed

If `old_categories` is empty (all nan categories) then `_recode_for_categories`
should return `codes.copy()` so that the writable flag is True.

- [x] closes #18051 
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Regression
description: Functionality that used to work in a prior pandas version
name: Categorical
description: Categorical Data Type

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=h1) Report
> Merging [#18279](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/69472f9ae9c645a0a5932c7c65b6efbdf66fbb5f?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `n/a`.

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

```diff
@@            Coverage Diff             @@
##           master   #18279      +/-   ##
==========================================
- Coverage    91.4%   91.38%   -0.02%     
==========================================
  Files         164      164              
  Lines       49878    49878              
==========================================
- Hits        45590    45581       -9     
- Misses       4288     4297       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.19% <ø> (ø)` | :arrow_up: |
| #single | `39.41% <ø> (-0.07%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY2F0ZWdvcmljYWwucHk=) | `95.75% <ø> (ø)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.8% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18279?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/18279?src=pr&el=footer). Last update [69472f9...b35f16e](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=h1) Report
> Merging [#18279](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/148ed63f9287cc55f7a2802da300b717d01cabe6?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #18279      +/-   ##
==========================================
- Coverage    91.4%   91.38%   -0.02%     
==========================================
  Files         164      164              
  Lines       49880    49880              
==========================================
- Hits        45592    45583       -9     
- Misses       4288     4297       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.19% <100%> (ø)` | :arrow_up: |
| #single | `39.42% <0%> (-0.07%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY2F0ZWdvcmljYWwucHk=) | `95.75% <100%> (ø)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.8% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18279?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/18279?src=pr&el=footer). Last update [148ed63...21734aa](https://codecov.io/gh/pandas-dev/pandas/pull/18279?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

synced off master and rebased to remove conflict in whatsnew
I would maybe add the original reported case of `pd.Series(pd.Categorical([np.nan])).nunique()` as a test as well? (eg in test_categorical.py::TestCategoricalAsBlock)
Works for me.

This issue is a bit too much under the hood for me to understand, so I can't speak to whether this is the best solution, but it works fine for me.
Hi,

I've looked into this again and IMO the tests should go in ``tests/series/test_analytics.py``, as that's where ``Series.unique`` and ``Series.nunique()`` are tested. These test failures actually are not happening on  ``Categorical``s themselves, o tests shouldn't go into ``test_categorical.py``.

So in method  ``test_value_counts_nunique`` I'd add these lines:

```python
# GH 18051
s = pd.Series(pd.Categorical([]))
assert s.nunique() == 0
s = pd.Series(pd.Categorical([np.nan]))
assert s.nunique() == 0
```

and in method ``test_unique`` I'd add:

```python
# GH 18051
s = pd.Series(pd.Categorical([]))
tm.assert_categorical_equal(s.unique(), pd.Categorical([]),
                            check_dtype=False)
s = pd.Series(pd.Categorical([np.nan]))
tm.assert_categorical_equal(s.unique(), pd.Categorical([np.nan]),
                            check_dtype=False)
```

and have no tests in ``test_categorical.py``.

@jreback , do you agree?
@topper-123 suggestions look reasonable.
@jreback, is it too late to get this in 0.21.1?

@ghasemnaddaf, if you don't have time to finish this up, I could open a new PR in order to get this into 0.21.1. Alternatively, I'd be very happy if you could finish this up for 0.21.1.
sorry @topper-123 im busy till weekend. Go for it thanks
superseded by #18436