BUG: Arithmetic operators in 1.5 throw RecursionError when dataframes have a mix of str and string typed levels · Issue #49769 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd from calendar import month_abbr import numpy as np

break_stuff = True

pd.show_versions()

midx = pd.MultiIndex.from_tuples([(yr, mo) for yr in ['2010', '2021'] for mo in [month_abbr[i] for i in range(1, 13)]], names=['MI1', 'MI2'])

if break_stuff: midx2 = midx.set_levels([midx.levels[0].astype('string'), midx.levels[1]]) # change outer level dtype to string else: midx2 = midx

idx = pd.Index(['a', 'b', 'c', 'd'], name='Category') data = np.random.rand(len(idx), len(midx)) x = pd.DataFrame(data, index=idx, columns=midx) y = pd.DataFrame(data, index=idx, columns=midx2)

z = y - x # boom!

Issue Description

If one has identically structured data frames where one has some column names as type str and the other of type string, doing any arithmentic operations using operators (confirmed with +, -, /, *) results in the infinite recursion: RecursionError: maximum recursion depth exceeded while calling a Python object. This did not happen with 1.4.X.

The recursion happens in frame_arith_method_with_reindex.py line 366, from what i can see.

Expected Behavior

Expected behaviour is that the operators would work, and in the above example to return a dataframe with all zeros.

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 7, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : English_United Kingdom.1252

pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.6
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.3.1
Cython : None
pytest : 7.2.0
hypothesis : None
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.5.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli : None
fastparquet : 0.8.3
fsspec : 2022.11.0
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 10.0.0
pyreadstat : None
pyxlsb : 1.0.10
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None