-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
inconsistent behaviour with .ix and setting values #2997
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
Comments
see returning a view vs copy your first operation is returning a copy, the set is in the copy in general if u are selecting a non-contiguous range (depends on exactly how numpy stores the data) you might get a copy |
jikes, seems really too much black magic for my taste: df4.ix[1:2].ix[:,"d"] = 7 #works
df4.ix[1:2,"b":] = 8 #works -> .ix setting directly on the df4?
df4.ix[1:2,"b":].ix[:,"d"] = 9 #works not... Wouldn't it be more consistent to use copy for every .ix/.iloc/.loc case? df5 = df4.ix[1:2]
df5["d"] = 10
df4 #boom |
chaining these call is only necesary in this case #2995 and you do need is there a case you really need and cannot do some other way? |
View or copy, it may seem like a box of chocolates ... I had in mind that when using label and/or slices a view is returned. Both In [49]: df4
Out[49]:
b c d
z a
1 a X 1 5
2 b Y 2 6
3 c X 3 7
4 d Y 4 8
In [50]: df4.ix[1:2,"b":] = 0
In [51]: df4
Out[51]:
b c d
z a
1 a 0 0 0
2 b 0 0 0
3 c X 3 7
4 d Y 4 8
In [52]: df4.ix[:,"d"] = 1
In [53]: df4
Out[53]:
b c d
z a
1 a 0 0 1
2 b 0 0 1
3 c X 3 1
4 d Y 4 1
In [54]: df4.ix[1:2,"b":].ix[:,"d"] = 100 # chaining both does not change df4 ???
In [55]: df4
Out[55]:
b c d
z a
1 a 0 0 1
2 b 0 0 1
3 c X 3 1
4 d Y 4 1 As already mentioned, chaining is not necessary here. In [67]: df4
Out[67]:
b c d
z a
1 a X 1 5
2 b Y 2 6
3 c X 3 7
4 d Y 4 8
In [68]: df4.ix[1:2, "d"] = 9
In [69]: df4
Out[69]:
b c d
z a
1 a X 1 9
2 b Y 2 9
3 c X 3 7
4 d Y 4 8 btw @JanSchulz, have you thought about using |
@lodagro: I think @jreback actually the "you don't know whether a view or copy is returned" is the problem for more than the double .ix case, as demonstrated by the assignment of a selection to a new variable: in one case changing a value in such a new dataframe changes the value in the original dataframe and in once case not. something along the line of
I would argue that .ix and every other selection mechanism should always return a copy. |
2013/3/9 JanSchulz [email protected]
|
That would kill performance in cases when you're selecting individual indices and contiguous slices in a read-only manner, though, which are the most common use cases for The only reasonable way to guarantee consistent semantics everywhere without completely sacrificing performance would be to implement some kind of view-but-copy-on-first-write wrapper over |
reslved by docs (and use of weak refs to update the caches) |
Using .ix twice and assigning a value as in
df.ix[...].ix[...] = b
works in the first case and not in the second:The text was updated successfully, but these errors were encountered: