Skip to content

BUG: find_common_type returning incorrect common type #55012

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

Closed
2 of 3 tasks
galipremsagar opened this issue Sep 5, 2023 · 4 comments
Closed
2 of 3 tasks

BUG: find_common_type returning incorrect common type #55012

galipremsagar opened this issue Sep 5, 2023 · 4 comments
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference

Comments

@galipremsagar
Copy link

Pandas version checks

  • 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 main branch of pandas.

Reproducible Example

In [2]: import pandas as pd
In [3]: from pandas.core.dtypes.cast import find_common_type
In [4]: import numpy as np
In [5]: find_common_type([np.dtype('int32'), np.dtype('bool')])
Out[5]: dtype('O')
In [6]: np.find_common_type([np.dtype('int32'), np.dtype('bool')], [])
Out[6]: dtype('int32')
In [7]: np.find_common_type([], [np.dtype('int32'), np.dtype('bool')])
Out[7]: dtype('int32')
In [8]: pd.__version__
Out[8]: '2.1.0'

Issue Description

I expect the common type for int32 & bool to be int32 rather than object dtype. This results in incorrect behavior of some operations like:

In [9]: idx = pd.Index([0, 1, 2, 3], dtype='int32')
In [10]: bool_idx = pd.Index([True, False, True])
In [11]: idx
Out[11]: Index([0, 1, 2, 3], dtype='int32')
In [12]: bool_idx
Out[12]: Index([True, False, True], dtype='bool')
In [13]: idx.union(bool_idx)
Out[13]: Index([0, 1, 1, 2, 3], dtype='object')

Expected Behavior

In [13]: idx.union(bool_idx)
Out[13]: Index([0, 1, 1, 2, 3], dtype='int32')

Installed Versions

INSTALLED VERSIONS

commit : ba1cccd
python : 3.10.2.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 2.1.0
numpy : 1.23.2
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 60.2.0
pip : 21.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.5.0
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 5, 2023
@galipremsagar
Copy link
Author

cc: @mroeschke

@mroeschke
Copy link
Member

Since bools and ints are meant to be distinct in pandas, I think the expected behavior would be (sort may be incorrect)

In [13]: idx.union(bool_idx)
Out[13]: Index([0, False, 1, True, 2, 3], dtype='object')

@mroeschke mroeschke added Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 5, 2023
@mroeschke
Copy link
Member

So the set ops issue is already known in #44000

But I think the find_common_type dtype behavior is intended

@mroeschke mroeschke added the Closing Candidate May be closeable, needs more eyeballs label Sep 6, 2023
@galipremsagar
Copy link
Author

Thanks @mroeschke ! Closing in favor of #44000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference
Projects
None yet
Development

No branches or pull requests

2 participants