BUG: Arithmetic operators in 1.5 throw RecursionError when dataframes have a mix of str and string typed levels

This issue has been created since 2022-11-18.

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
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

MarcoGorelli wrote this answer on 2022-11-18

Thanks @dk1010101 for the report

git bisect show:

443f2b1 is the first bad commit

BUG: Multiindex.equals not commutative for ea dtype (#46047)

https://www.kaggle.com/code/marcogorelli/pandas-regression-example?scriptVersionId=111378614

MarcoGorelli wrote this answer on 2022-11-18
More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-12-07
Star Count 36164
Watcher Count 1118
Fork Count 15472
Issue Count 3683

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Carret is always put at the end of text after transforming block using slash syntax 2 2022-08-02 2022-11-20
Switch to a declarative way of using numbers in the tests 2 2021-05-17 2022-11-25
Possible ConfigMapping issue with sibling/nested maps 4 2021-12-15 2022-11-01
show_psi4 not working 2 2021-05-21 2022-11-24
Best Practices Question re: Updates 1 2022-05-10 2022-10-25
Question: Are the claims made on SMACC about BT valid for this implementation? 5 2021-11-16 2022-10-15
Action Required: Fix Renovate Configuration 0 2021-01-21 2022-11-20
[REQ] xvc codec support 0 2021-09-09 2022-11-11
有计划实现类似python版的基于HMM的词性标注能力吗? 1 2021-07-20 2022-11-25
能应用在qt上么?我vs调试没问题,但是放在qt中就出现错误 11 2019-09-30 2022-11-25
How to turn on runtime debug output 0 2022-02-11 2022-10-08
[feature request] export several STL files from a single slvs file 4 2021-12-29 2022-09-30
@Collation can infer locale from system default 0 2021-07-30 2022-11-26
"Inconsistent Network" error when using RegTest network 3 2019-03-19 2022-11-15
Camel-case of property in main page's s doc 1 2021-03-02 2022-11-06
gphoto2 --capture-image-and-download fails 1 2022-03-20 2022-10-24
How to get the status of focus for canon 1500D 2 2022-03-18 2022-10-24
[Question] No auto login after update 10 2022-11-06 2022-11-10
Specify timeout parameter 3 2020-06-22 2022-11-20
[Eternal] Proofread Tarantool changelogs for 2.10.0-rc1 1 2021-09-10 2022-10-14
Bullet#88 Warning causes LiquidTurret to crash mod 3 2022-04-26 2022-11-22
where is this sdk Technical architecture description? 1 2021-03-09 2022-11-27
Error starting OpenGL context on Ubuntu with Intel graphics 8 2021-01-15 2022-11-22
Tracks on the backend should allow for easier testing 5 2022-01-27 2022-02-07
preview help 21 2021-12-09 2022-01-11
Cannot install Istio from main branch builds. 0 2021-10-19 2022-11-13
is restricted to (DUO:00000010) definition and use in DUO mismatch 0 2021-03-08 2022-11-22
ROCK-Kernel-Driver build failing 3 2021-05-05 2022-11-19
Unable to take screenshot warning on Safari 15.1 macOS 12.0.1 1 2021-11-30 2022-11-24
Unknown error : Uncaught TypeError: Cannot read properties of null (reading 'CodeMirror') 3 2022-11-18 2022-12-07
Kafka Input Plugin not working properly 1 2020-06-05 2022-12-03
Contributors page: Needs a search option 2 2021-10-27 2022-10-14
rror configuring higher education portal 0 2022-06-14 2022-11-29
build: bump peter-evans/create-pull-request from 3.10.0 to 3.10.1 0 2021-08-23 2022-11-28
button side by side 1 2021-01-14 2022-01-15
LongformerForSequenceClassification explanation 1 2021-08-11 2022-11-12
The availability of Longformer-tiny? 0 2021-06-29 2022-11-12
getting error of same class of exoplayer in android 11 2021-07-14 2022-12-03
Add https://anon.sx/ 14 2021-02-06 2022-08-03
There were insufficent NCs for the requested operation 1 2021-07-01 2022-11-16
Remove @ExperimentalTime 0 2021-11-23 2022-09-29
[Bug] YMDK YMD75V3 Caps lock LED pin does not work. 0 2022-10-07 2022-11-10
Unify ConversionException and ImplicitCastException 0 2022-09-26 2022-11-10
Customizable 2 2021-04-29 2022-01-16
インストール画面のSVGアイコンが表示されていない 1 2022-08-20 2022-09-09
The method TimeSeriesData() cannot work with pandas.Timestamp in particular format 0 2021-08-18 2022-11-22
App is crashing after update from version 4.1 to 5.1 3 2020-03-26 2022-11-15
Don't list unattachable volumes as "unattached" in mount timed out event log 3 2022-10-20 2022-11-02
Cannot run patchesStrategicMerge on CRDs schema under Versions array 9 2022-10-20 2022-11-02
Naming inconsistency for inlineTagProcessor 0 2018-06-14 2022-11-27