Skip to content

BUG? when incrementing .loc[]-view #3697

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 May 27, 2013 · 6 comments
Closed

BUG? when incrementing .loc[]-view #3697

immerrr opened this issue May 27, 2013 · 6 comments

Comments

@immerrr
Copy link
Contributor

immerrr commented May 27, 2013

Am I doing something wrong with indexing here?

PS. on the second thought it looks related to #3492, but I don't have a dev environment at hand to test and it fails for some reason when trying to build pandas via pip. If that's the case and it's already fixed, my apologies and feel free to close this one.

Steps to reproduce:

>>> df = DataFrame(index=np.arange(10))
>>> df['foobar'] = False
>>> df['a'] = np.arange(10)
>>> df
  foobar  a
0  False  0
1  False  1
2  False  2
3  False  3
4  False  4
5  False  5
6  False  6
7  False  7
8  False  8
9  False  9
# This works as expected
>>> df.a[df.a % 2 == 0] += 10
>>> df
  foobar   a
0  False  10
1  False   1
2  False  12
3  False   3
4  False  14
5  False   5
6  False  16
7  False   7
8  False  18
9  False   9
# This doesn't work, but expected due to copying on bool-indexing
>>> df[df.a % 2 == 0].a += 100
>>> df
  foobar   a
0  False  10
1  False   1
2  False  12
3  False   3
4  False  14
5  False   5
6  False  16
7  False   7
8  False  18
9  False   9
# But what does this?
>>> df.loc[df.a % 2 == 0, 'a'] += 1000
>>> df
  foobar     a
0  False  1010
1  False     1
2  False  1010
3  False     3
4  False  1010
5  False     5
6  False  1010
7  False     7
8  False  1010
9  False     9

>>> pandas.__version__
'0.11.0'
@cpcloud
Copy link
Member

cpcloud commented May 27, 2013

don't think this is a bug, see here

@immerrr
Copy link
Contributor Author

immerrr commented May 28, 2013

@cpcloud , the page you've referenced mentions two out of three cases in my snippet, which, if you followed along the comments, were expected in the first place. It is in the 3rd case that where the magic starts: it's not a copy as changes are propagated to df.a, but the += operator goes nuts and instead of increasing every odd (even in 0-based notation) element by 1000, it only increases the first one and assigns the rest to it.

@cpcloud
Copy link
Member

cpcloud commented May 28, 2013

@immerrr whoops sorry didn't look at your last example close enough...i see now.

@cpcloud
Copy link
Member

cpcloud commented May 28, 2013

@immerrr this is fixed in git master

@jreback
Copy link
Contributor

jreback commented May 28, 2013

@immerrr, @cpcloud is right, this was fixed in master, #3533, introduced in 0.11.0, but
fixed in 0.11.1 (upcoming)

In [1]: df = DataFrame(index=np.arange(10))

In [2]: df['foobar'] = False

In [3]: df['a'] = np.arange(10)

In [4]: 

In [4]: df
Out[4]: 
  foobar  a
0  False  0
1  False  1
2  False  2
3  False  3
4  False  4
5  False  5
6  False  6
7  False  7
8  False  8
9  False  9

In [5]: df.loc[df.a % 2 == 0, 'a'] += 1000

In [6]: df
Out[6]: 
  foobar     a
0  False  1000
1  False     1
2  False  1002
3  False     3
4  False  1004
5  False     5
6  False  1006
7  False     7
8  False  1008

@immerrr
Copy link
Contributor Author

immerrr commented May 28, 2013

@cpcloud @jreback
Awesome, thanks, looking forward to see it in pypi index. Closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants