BUG: Return DataFrame could not preserve index dtype when using .combine_first
with categorical index
#36568
Labels
.combine_first
with categorical index
#36568
[ √] 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.
Code Sample, a copy-pastable example
Problem description
I use df.combine_first(...) to add a column to an existing dataframe while extending the index in case an index value does not exist in the target dataframe. One level of indexes is
Categorical
Dtype which came fromdf = df.set_index(['x', 'a'], drop=True)
. I expected theCategorical
dtype index could be restored bydf3 = df2.reset_index(drop=False)
aftercombine_first
. However, if the MultIindex of the dataframe usesCategorical
values in their index, theCategorical
dtype info in the return dataframe lost .Expected Output
df2 level 0 index has
CategoricalDtype
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : f2ca0a2
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_AU.UTF-8
LOCALE : en_AU.UTF-8
pandas : 1.1.1
numpy : 1.18.4
pytz : 2019.3
dateutil : 2.8.1
pip : 20.2.3
setuptools : 47.3.1
Cython : 0.29.17
pytest : 5.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 0.9.6
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.3 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : 1.3.1
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 1.8.6
pandas_gbq : None
pyarrow : 0.17.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.12
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: