REGR: Assignment to MultiIndex fails for pandas 1.0.0. · Issue #31449 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

This problem occurred with pandas 1.0.0 py37he350917_0 conda-forge, but did not before or at least not since 0.24.

import pandas as pd import numpy as np

index = pd.MultiIndex.from_tuples([("a", "c"), ("b", "x"), ("a", "d")], names=["l1", "l2"]) df = pd.DataFrame(index=index, data=np.arange(3), columns=["e"]) df.loc["a", "e"] = np.arange(99, 101)

Problem description

The error message is the following

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-b62e661ba0e4> in <module>
----> 1 df.loc["a", "e"] = np.arange(99, 101)

C:\tools\miniconda3\envs\respy\lib\site-packages\pandas\core\indexing.py in __setitem__(self, key, value)
    668             key = com.apply_if_callable(key, self.obj)
    669         indexer = self._get_setitem_indexer(key)
--> 670         self._setitem_with_indexer(indexer, value)
    671 
    672     def _validate_key(self, key, axis: int):

C:\tools\miniconda3\envs\respy\lib\site-packages\pandas\core\indexing.py in _setitem_with_indexer(self, indexer, value)
    927                     # we can directly set the series here
    928                     # as we select a slice indexer on the mi
--> 929                     idx = index._convert_slice_indexer(idx)
    930                     obj._consolidate_inplace()
    931                     obj = obj.copy()

C:\tools\miniconda3\envs\respy\lib\site-packages\pandas\core\indexes\base.py in _convert_slice_indexer(self, key, kind)
   2917 
   2918         # potentially cast the bounds to integers
-> 2919         start, stop, step = key.start, key.stop, key.step
   2920 
   2921         # figure out if this is a positional indexer

AttributeError: 'numpy.ndarray' object has no attribute 'start'

If the MultiIndex would have been sorted, key in the last part of the traceback or indexer in the first part would have been a slice and everything is fine. Since the MultiIndex is unsorted key or indexer are a boolean array which has no start stop attributes.

I am sorry for not digging further, but I have not inspected pandas internals before.

Expected Output

         e
l1 l2     
a  c    99
b  x     1
a  d   100

Output of pd.show_versions()

INSTALLED VERSIONS

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

pandas : 1.0.0
numpy : 1.17.5
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0.post20200119
Cython : None
pytest : 5.3.5
hypothesis : None
sphinx : 2.3.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.11.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : None
scipy : 1.3.1
sqlalchemy : 1.3.13
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.46.0