Repository: pandas-dev/pandas

Issue:
Segmentation fault after set_categories()
#### Code Sample, a copy-pastable example if possible

```python
>>> import pandas as pd
>>> s = pd.Series(['A', 'B']).astype('category').cat.set_categories(['A'], rename=True)
>>> print(s)
Segmentation fault
```
#### Problem description

The segmentation fault occurs,
 - if `set_categories()` is called with fewer new categories than original categories
 - and rename is set true
 - as soon as the data is accessed.

#### Output of ``pd.show_versions()``

<details>


INSTALLED VERSIONS
------------------
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-957.1.3.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 40.6.3
Cython: None
numpy: 1.15.4
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None


</details>



Discussion:
Thanks for the report. Investigation and PRs are always welcome!
Simplifying a little, the same thing happens when working directly with a `Categorical`:
```python
pd.Categorical(['A', 'B']).set_categories(['A'], rename=True)
```

Resolving patch:
diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py
--- a/pandas/core/arrays/categorical.py
+++ b/pandas/core/arrays/categorical.py
@@ -852,9 +852,9 @@ def set_categories(self, new_categories, ordered=None, rename=False,
             if (cat.dtype.categories is not None and
                     len(new_dtype.categories) < len(cat.dtype.categories)):
                 # remove all _codes which are larger and set to -1/NaN
-                self._codes[self._codes >= len(new_dtype.categories)] = -1
+                cat._codes[cat._codes >= len(new_dtype.categories)] = -1
         else:
-            codes = _recode_for_categories(self.codes, self.categories,
+            codes = _recode_for_categories(cat.codes, cat.categories,
                                            new_dtype.categories)
             cat._codes = codes
         cat._dtype = new_dtype


Test patch:
diff --git a/pandas/tests/arrays/categorical/test_api.py b/pandas/tests/arrays/categorical/test_api.py
--- a/pandas/tests/arrays/categorical/test_api.py
+++ b/pandas/tests/arrays/categorical/test_api.py
@@ -310,6 +310,13 @@ def test_set_categories_many(self, values, categories, new_categories,
         result = c.set_categories(new_categories, ordered=ordered)
         tm.assert_categorical_equal(result, expected)
 
+    def test_set_categories_rename_less(self):
+        # GH 24675
+        cat = Categorical(['A', 'B'])
+        result = cat.set_categories(['A'], rename=True)
+        expected = Categorical(['A', np.nan])
+        tm.assert_categorical_equal(result, expected)
+
     def test_set_categories_private(self):
         cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd'])
         cat._set_categories(['a', 'c', 'd', 'e'])