-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: reorder_categories
with inplace=True
is not changing the dtype.categories
#43232
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
This seems to be the same for inplace operation with In [1]: import pandas as pd
In [2]: sr = pd.Series(['a', 'b', 'c'], dtype='category')
In [3]:
In [3]: sr
Out[3]:
0 a
1 b
2 c
dtype: category
Categories (3, object): ['a', 'b', 'c']
In [4]: new = sr.cat.remove_categories(['a'])
In [5]: new
Out[5]:
0 NaN
1 b
2 c
dtype: category
Categories (2, object): ['b', 'c']
In [6]: new.dtype.categories
Out[6]: Index(['b', 'c'], dtype='object')
In [7]: sr.cat.remove_categories(['a'], inplace=True)
pandas/core/arrays/categorical.py:2630: FutureWarning: The `inplace` parameter in pandas.Categorical.remove_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 [8]: sr
Out[8]:
0 NaN
1 b
2 c
dtype: category
Categories (2, object): ['b', 'c']
In [9]: sr.dtype.categories
Out[9]: Index(['a', 'b', 'c'], dtype='object') |
take |
seems to be working as expected in pandas v'1.2.4' `
` |
on 1.2.5 and earlier, sr.dtype is maybe it should return an ordered categorical dtype. as will bisect shortly for further context. |
changing and reverting blocks.py still gives the same error |
Also looks like the warning message was copy/pasted from remove_unused_categories and needs to be updated |
When I change Block.dtype from cache_readonly to property, that seems to fix it
Are you seeing something different? Note: if you want to make a PR to do this, only override it on CategoricalBlock instead of on Block. |
@jbrockmendel xref #43334 (comment) Is that what you are suggesting? I'm not a advocate of 11th hour changes as the longer things sit in master the more chance of catching any problems. I'll move this to 1.3.4 |
Yes. |
Hi @LobRockyl - just checking if you're still working on this.. |
Yeah was a lil caught up. Will raise a PR asap |
Hey changing blocks does fix it but didn't get what u meant by "override it on CategoricalBlock instead of on Block." |
Instead of changing Block.dtype, define CategoricalBlock.dtype |
So basically making the changes in categorical.py right? There is no dtype class in categorical.py so should I have to include it and change it? |
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
Problem description
When we perform
reorder_categories
operation as an inplace op, thecategories
attribute ofCategoricalDtype
is not updated.Expected Output
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
The text was updated successfully, but these errors were encountered: