Skip to content

BUG: pd.concat changes multiindex dtype of string to object #54745

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
3 tasks done
dalejung opened this issue Aug 25, 2023 · 3 comments · Fixed by #54778
Closed
3 tasks done

BUG: pd.concat changes multiindex dtype of string to object #54745

dalejung opened this issue Aug 25, 2023 · 3 comments · Fixed by #54778
Labels
Bug Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@dalejung
Copy link
Contributor

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

import pandas as pd

# create two frames with [string, Int64] multi index.
mi_df = pd.DataFrame({
    'strings': ['HELLO FREN'],
    'ints': [0],
    'floats': [0.1],
}).convert_dtypes().set_index(['strings', 'ints'])

# print(mi_df.index.dtypes)
# strings    string[python]
# ints                Int64

mi_df2 = pd.DataFrame({
    'strings': ['BYE FREN'],
    'ints': [1],
    'floats': [0.1],
}).convert_dtypes().set_index(['strings', 'ints'])

# print(mi_df2.index.dtypes)
# strings    string[python]
# ints                Int64

# concatting index where strings are same retains dtype
good_concat = pd.concat([mi_df, mi_df.copy()])
assert good_concat.index.dtypes['strings'] == 'string'

# otherwise the string level gets turned into object
bad_concat = pd.concat([mi_df, mi_df2])
# ERROR: this should be 'string' and not object.
assert bad_concat.index.dtypes['strings'] == object
# print(bad_concat.index.dtypes)
# strings    object
# ints        Int64

Issue Description

Regression found on main. The dtypes of the MultiIndex shouldn't change in a concat with identical index dtypes.

This works as expected on 1.5.3.

Expected Behavior

Dtypes remain unchanged in concatenated frame.

Installed Versions

INSTALLED VERSIONS

commit : 8db9f11ed3d87310ae46fe77ad89041973a4893d
python : 3.11.3.final.0
python-bits : 64
OS : Linux
OS-release : 6.4.9-arch1-1
Version : #1 SMP PREEMPT_DYNAMIC Tue, 08 Aug 2023 22:14:05 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.0rc0+97.g8db9f11ed3
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.3.0
pip : 22.2.2
Cython : 0.29.33
pytest : 7.4.0
hypothesis : 6.47.1
sphinx : 6.2.1
blosc : 1.11.1
feather : None
xlsxwriter : 3.1.0
lxml.etree : 4.9.2
html5lib : 1.1
pymysql : 1.0.3
psycopg2 : 2.9.6
jinja2 : 3.1.2
IPython : 8.15.0.dev
pandas_datareader : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat: None
fastparquet : 2023.4.0
fsspec : 2023.5.0
gcsfs : 2023.5.0
matplotlib : 3.7.1
numba : 0.57.0
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 14.0.0.dev14+g72811b13e
pyreadstat : 1.2.1
pyxlsb : 1.0.10
s3fs : 2023.5.0
scipy : 1.10.1
sqlalchemy : 2.0.14
tables : 3.8.0
tabulate : 0.9.0
xarray : 2023.5.0
xlrd : 2.0.1
zstandard : 0.21.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

@dalejung dalejung added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 25, 2023
@phofl
Copy link
Member

phofl commented Aug 25, 2023

Does this work on 2.0 as well?

@dalejung
Copy link
Contributor Author

In [9]: bad_concat.index.dtypes
Out[9]:
strings    string[python]
ints                Int64
dtype: object

In [10]: print(pd.__version__)
2.0.3

Works as expected in 2.0.3.

@phofl phofl added Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 27, 2023
@phofl phofl added this to the 2.1 milestone Aug 27, 2023
@phofl
Copy link
Member

phofl commented Aug 27, 2023

Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants