Skip to content

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

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

Closed
immerrr opened this issue Nov 19, 2014 · 8 comments · Fixed by #41389
Closed
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@immerrr
Copy link
Contributor

immerrr commented Nov 19, 2014

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.

@jreback
Copy link
Contributor

jreback commented Nov 19, 2014

This was prob never tested properly. That said for loc/ix. You could step 'logically', meaning you step the 'levels', but not sure if that is useful.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Nov 19, 2014
@jreback jreback added this to the 0.15.2 milestone Nov 19, 2014
@jreback
Copy link
Contributor

jreback commented Nov 30, 2014

@immerrr able to get to this soon? otherwise I will bump to next version

@immerrr
Copy link
Contributor Author

immerrr commented Nov 30, 2014

My schedule is flaky for ~2 more weeks, sorry.

@jreback
Copy link
Contributor

jreback commented Nov 30, 2014

np...take your time

@jreback jreback modified the milestones: 0.16.0, 0.15.2 Nov 30, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@jreback jreback modified the milestones: Next Major Release, 0.22.0 Dec 9, 2017
@jreback jreback modified the milestones: 0.23.0, Next Major Release Apr 14, 2018
@wesm
Copy link
Member

wesm commented Jul 6, 2018

I just looked at this. iloc behaves properly, but loc does not

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

In [9]: s.iloc[::4] = 100

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

In [11]: s.loc[::4] = 200

In [12]: s
Out[12]: 
0  a    200
   b    200
   c    200
   d    200
   e    200
1  a      5
   b      6
   c      7
   d    100
   e      9
dtype: int64

@simonjayhawkins simonjayhawkins changed the title BUG: loc/iloc/ix.__setitem__ over MultiIndex with slice key and step != 1 does strange things BUG: loc.__setitem__ over MultiIndex with slice key and step != 1 does strange things Dec 12, 2019
@simonjayhawkins
Copy link
Member

I just looked at this. iloc behaves properly, but loc does not

changed title to reflect this and that .ix is deprecated.

@simonjayhawkins
Copy link
Member

could add a test to confirm the .iloc behavior.

@simonjayhawkins simonjayhawkins added the Needs Tests Unit test(s) needed to prevent regressions label Dec 12, 2019
@mroeschke
Copy link
Member

Looks like both loc and iloc work consistently now

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

In [11]: s.loc[::4] = 200

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

In [13]: s.iloc[::4] = 100

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

In [15]: pd.__version__
Out[15]: '1.3.0.dev0+1282.g33fec602ff'

@mroeschke mroeschke removed the Bug label Apr 11, 2021
@mroeschke mroeschke added good first issue and removed Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Apr 11, 2021
@simonjayhawkins simonjayhawkins modified the milestones: Contributions Welcome, 1.3 May 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
6 participants