Skip to content

BUG: reorder_categories with inplace=True is not changing the dtype.categories #43232

Closed
@galipremsagar

Description

@galipremsagar
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

In [1]: import pandas as pd

In [2]: pd.__version__
Out[2]: '1.3.2'

In [3]: sr = pd.Series(['a', 'b', 'c'], dtype='category')

In [4]: sr
Out[4]: 
0    a
1    b
2    c
dtype: category
Categories (3, object): ['a', 'b', 'c']

In [5]: new = sr.cat.reorder_categories(['c', 'b', 'a'])

In [6]: new
Out[6]: 
0    a
1    b
2    c
dtype: category
Categories (3, object): ['c', 'b', 'a']

In [7]: new.dtype.categories
Out[7]: Index(['c', 'b', 'a'], dtype='object')

In [8]: sr.cat.reorder_categories(['c', 'b', 'a'], inplace=True)
pandas/core/arrays/categorical.py:2630: FutureWarning: The `inplace` parameter in pandas.Categorical.reorder_categories is deprecated and will be removed in a future version. Removing unused categories will always return a new Categorical object.
  res = method(*args, **kwargs)

In [9]: sr
Out[9]: 
0    a
1    b
2    c
dtype: category
Categories (3, object): ['c', 'b', 'a']

In [10]: sr.dtype.categories
Out[10]: Index(['a', 'b', 'c'], dtype='object')

Problem description

When we perform reorder_categories operation as an inplace op, the categories attribute of CategoricalDtype is not updated.

Expected Output

In [7]: sr.dtype.categories
Out[7]: Index(['c', 'b', 'a'], dtype='object')

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 5f648bf
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-27-generic
Version : #29~20.04.1-Ubuntu SMP Wed Aug 11 15:58:17 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.2
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 57.4.0
Cython : 0.29.24
pytest : 6.2.4
hypothesis : 6.15.0
sphinx : 4.1.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.26.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.07.0
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1

Metadata

Metadata

Labels

BugCategoricalCategorical Data TypeRegressionFunctionality that used to work in a prior pandas version

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions