Skip to content

BUG: AssertionError when slicing MultiIndex and setting value of pandas.Series #34870

Closed
@epizzigoni

Description

@epizzigoni

Code Sample

>>> import numpy as np
... import pandas as pd
... 
... arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
...           ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
... tuples = list(zip(*arrays))
... index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
... 
... s = pd.Series(np.random.randn(8), index=index)
>>>s
first  second
bar    one      -0.592576
       two      -0.054856
baz    one      -0.169856
       two      -0.712601
foo    one       2.180688
       two      -0.032646
qux    one      -0.729950
       two       0.883029
dtype: float64

>>> s[('baz', 'one'):('foo', 'two')] = 4  # This works!
>>> s
first  second
bar    one      -0.592576
       two      -0.054856
baz    one       4.000000
       two       4.000000
foo    one       4.000000
       two       4.000000
qux    one      -0.729950
       two       0.883029
dtype: float64

>>> s.loc[('baz', 'one'):('foo', 'two')] = 4  # This gives the error!
Traceback (most recent call last):
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('baz', 'one')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 2890, in _get_level_indexer
    start = level_index.get_loc(key.start)
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('baz', 'one')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexing.py", line 670, in __setitem__
    indexer = self._get_setitem_indexer(key)
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexing.py", line 641, in _get_setitem_indexer
    return ax.get_loc(key)
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 2662, in get_loc
    loc = self._get_level_indexer(key, level=0)
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 2903, in _get_level_indexer
    key.start, key.stop, key.step, kind="loc"
  File "/Users/edoardo/miniconda3/envs/my37/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 4717, in slice_indexer
    raise AssertionError("Start slice bound is non-scalar")
AssertionError: Start slice bound is non-scalar

Problem description

I'm trying to slice and set values of a pandas Series but using the loc function does not work. I can do it by simply using [] and using loc if the Series is first converted into a DataFrame.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Darwin
OS-release : 19.5.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 1.0.4
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 47.3.0.post20200616
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.9.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexRelated to the Index class or subclassesMultiIndex

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions