Skip to content
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

BUG: Concat does not preserve category if lists of categories do not match. #42840

Open
2 of 3 tasks
yohplala opened this issue Aug 1, 2021 · 1 comment
Open
2 of 3 tasks
Labels
Bug Categorical Categorical Data Type Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@yohplala
Copy link

yohplala commented Aug 1, 2021

  • 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

import pandas as pd
dfx = pd.DataFrame({'a':[1,2,3], 'cat':['a', 'b', 'a']}).astype({'cat':'category'})
dfy = pd.DataFrame({'a':[3,4,5], 'cat':['a', 'a', 'a']}).astype({'cat':'category'}) # no 'b' here
dfz=pd.concat([dfx,dfy])

dfz['cat']
Out[55]: 
0    a
1    b
2    a
3    a
4    a
5    a
Name: cat, dtype: object

Problem description

I am expecting (erroneously?) that resulting 'cat' column should be the merged categories of 'dfx' and 'dfy'.

Expected Output

dfz['cat']
Out[58]: 
0    a
1    b
2    a
3    a
4    a
5    a
Name: cat, dtype: category
Categories (2, object): ['a', 'b']

Output of pd.show_versions()

INSTALLED VERSIONS

commit : c7f7443
python : 3.8.8.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-63-generic
Version : #71~20.04.1-Ubuntu SMP Thu Jul 15 17:46:08 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.1
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.2
pip : 21.1.3
setuptools : 52.0.0.post20210125
Cython : 0.29.24
pytest : 6.2.4
hypothesis : None
sphinx : 4.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.07.0
fastparquet : 0.7.0
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : 0.18.2
xlrd : None
xlwt : None
numba : 0.53.1

@yohplala yohplala added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 1, 2021
@yohplala
Copy link
Author

yohplala commented Aug 1, 2021

Root cause possibly shared with ticket #14016 ?

@yohplala yohplala changed the title BUG: Concat does not preserve category if list of categories does not match. BUG: Concat does not preserve category if lists of categories do not match. Aug 1, 2021
@mroeschke mroeschke added Categorical Categorical Data Type Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants