-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: pd.replace changes category dtype to object when using a dict to replace values #35268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is true for other input dtypes as well, such as the new string dtype, and is demonstrable with
I believe the issue is with the underlying pandas/pandas/core/internals/blocks.py Lines 754 to 762 in 3b1d4f1
Note the comment about extension dtypes, which seems particularly applicable. |
I also meant to point out that using two-arg |
The Period dtype also seems to suffer this issue: #34871. |
See #36867 for an idea of how to fix this. Basically I think that |
closed by #44940 |
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
Currently, the category dtypes are lost when we use pandas replace. This loss of category dtype is observed only when a dictionary is used to replace multiple values.
I was able to recreate the issue for
pd.__version__ == '1.1.0.dev0+2073.g280efbfcc'
as well. Also, please see related issue: #23305Expected Output
I would expect the categorical columns to keep their category dtype after replace is called on the dataframe. Thanks!
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.5.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.0.5
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 19.0.3
setuptools : 40.8.0
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.3
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None
The text was updated successfully, but these errors were encountered: