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: Concatenation of two objects of the category type gives an object of the object type #43477

Closed
2 of 3 tasks
leihuang opened this issue Sep 9, 2021 · 1 comment
Closed
2 of 3 tasks
Labels
Categorical Categorical Data Type Duplicate Report Duplicate issue or pull request Needs Triage Issue that has not been reviewed by a pandas team member Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@leihuang
Copy link

leihuang commented Sep 9, 2021

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

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

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

Reproducible Example

import pandas as pd

a = pd.Series(['A', 'B'], dtype='category')
b = pd.Series(['C', 'D'], dtype='category')

print(a.dtypes, b.dtypes, pd.concat((a,b)).dtypes)

Issue Description

The output of the codes above is:

category category object

Expected Behavior

I expect the output to be:

category category category

Concatenation of two objects of the same type should give an object of that type. The current behavior gave me some unexpected results that took me a while to figure out.

Installed Versions

INSTALLED VERSIONS

commit : 2cb9652
python : 3.7.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.120+
Version : #1 SMP Wed Aug 18 10:20:32 PDT 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.19.5
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.2
setuptools : 49.6.0.post20210108
Cython : 0.29.23
pytest : 6.2.4
hypothesis : 6.14.1
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.24.1
pandas_datareader: 0.9.0
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 2021.06.1
fastparquet : None
gcsfs : 2021.06.0
matplotlib : 3.4.2
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 4.0.0
pyxlsb : None
s3fs : 2021.06.1
scipy : 1.6.3
sqlalchemy : 1.4.18
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.18.2
xlrd : None
xlwt : None
numba : 0.53.1

@leihuang leihuang added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 9, 2021
@mzeitlin11
Copy link
Member

Thanks for the report @leihuang. Going to close as a duplicate of #42840. The reason this gives object type is that the categories don't match, so the common type between them defaults to object (for now). So if what you're expecting is a union of categories (eg ABCD in this case), you could explicitly include all categories when instantiating the dtype so that types match

@mzeitlin11 mzeitlin11 added Categorical Categorical Data Type Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Bug labels Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Duplicate Report Duplicate issue or pull request Needs Triage Issue that has not been reviewed by a pandas team member Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants