Skip to content
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

REGR: Assignment to MultiIndex fails for pandas 1.0.0. #31449

Closed
tobiasraabe opened this issue Jan 30, 2020 · 5 comments · Fixed by #31631
Closed

REGR: Assignment to MultiIndex fails for pandas 1.0.0. #31449

tobiasraabe opened this issue Jan 30, 2020 · 5 comments · Fixed by #31631
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@tobiasraabe
Copy link

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

@jorisvandenbossche
Copy link
Member

@tobiasraabe Thanks for the report! I can confirm this is a regression also compared to 0.25

@jorisvandenbossche jorisvandenbossche added the Regression Functionality that used to work in a prior pandas version label Jan 30, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.0.1 milestone Jan 30, 2020
@jorisvandenbossche jorisvandenbossche added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jan 30, 2020
@jorisvandenbossche
Copy link
Member

cc @jbrockmendel might be related to #27883 (PR that touched the related function in the traceback)

@jorisvandenbossche jorisvandenbossche changed the title Assignment to MultiIndex fails for pandas 1.0.0. REGR: Assignment to MultiIndex fails for pandas 1.0.0. Jan 30, 2020
@tobiasraabe
Copy link
Author

Glad I did not waste your time :). I use df.loc[pd.IndexSlice["a", :], "e"] = np.arange(99, 101) as a workaround.

@jbrockmendel
Copy link
Member

on indexing.py L896 we shouldn't be calling idx = index._convert_slice_indexer(idx) unless we have a slice. Adding a if isinstance(idx, slice): check before that line appears to fix the error in the OP.

@jorisvandenbossche
Copy link
Member

Yep, indeed, it's what _convert_slice_indexer did itself before #27883, but it makes sense to do the check before calling the function

TomAugspurger added a commit to TomAugspurger/pandas that referenced this issue Feb 3, 2020
jbrockmendel pushed a commit that referenced this issue Feb 3, 2020
* REGR: Fixed setitem with MultiIndex

Closes #31449

* 32-bit compat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants