Skip to content

BUG: loc with empty multiindex raises exception #36936

Closed
@egorvavilov

Description

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

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

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


Hi, last line of snippet throws an exception ValueError: operands could not be broadcast together with shapes (0,) (2,) (0,)

import pandas as pd

arrays = [['val1', 'val1', 'val2'], ['val1', 'val1', 'val2']]
index = pd.MultiIndex.from_arrays(arrays, names=('idx1', 'idx2'))

df = pd.DataFrame([1, 2, 3], index=index, columns=['value'])

df.loc[df[df['value'] == 0].index, :] = 4

Problem description

.loc raises error in case dataframe has duplicates in index. df[df['value'] == 0].index produces MultiIndex([], names=['idx1', 'idx2']) and it seems to be absolutely valid to put it into .loc method.

Expected Output

I expect .loc operation to make no changes to original df cause multiindex in .loc is empty.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.7.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.17763
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.1.3
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 46.0.0
Cython : None
pytest : 6.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.3.0
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.5 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : 7.14.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : 3.0.4
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : 1.0.6
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.17
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
numba : None

Metadata

Metadata

Assignees

Labels

IndexingRelated to indexing on series/frames, not to indexes themselvesNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions