Skip to content

BUG: loc.__setitem__ over MultiIndex with slice key and step != 1 does strange things #8856

Closed
@immerrr

Description

@immerrr

Here's how it looks like:

In [11]: s = pd.Series(np.arange(10), pd.MultiIndex.from_product(([0, 1], list('abcde'))))

In [12]: s
Out[12]: 
0  a    0
0  b    1
0  c    2
0  d    3
0  e    4
1  a    5
1  b    6
1  c    7
1  d    8
1  e    9
dtype: int64

In [13]: s.iloc[::4] = 100; s
Out[13]: 
0  a    100
0  b    100
0  c    100
0  d    100
0  e    100
1  a      5
1  b      6
1  c      7
1  d      8
1  e      9
dtype: int64

In [14]: s.loc[::4] = 200; s
Out[14]: 
0  a    200
0  b    200
0  c    200
0  d    200
0  e    200
1  a      5
1  b      6
1  c      7
1  d      8
1  e      9
dtype: int64

In [15]: s.ix[::4] = 300; s
Out[15]: 
0  a    300
0  b    300
0  c    300
0  d    300
0  e    300
1  a      5
1  b      6
1  c      7
1  d      8
1  e      9
dtype: int64

Regular setitem works fine:

In [16]: s[::4] = 10000; s
Out[16]: 
0  a    10000
0  b      300
0  c      300
0  d      300
0  e    10000
1  a        5
1  b        6
1  c        7
1  d    10000
1  e        9
dtype: int64

I have some ideas about iloc refactoring that will fix that for iloc, but loc/ix will still be affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions