Understand A value is trying to be set on a copy of a slice from a DataFrame
up vote
3
down vote
favorite
I am trying to understand when do I get a copy vs view when using .loc method to index the dataframe. Getting a view to me means any changes on view will be carried over to the original df. I expect using .loc gives me a copy.
In below example, I am indexing the df using the same method, but one gives me a copy, the other one gives me a view.
Can someone point out what I am missing?
a = pd.DataFrame([[1,2, 1], [2, 4,1], [3,4,1]])
b = a.loc[:, 1:3]
b.loc[:, 'tag'] = 'test' # this is a copy?
b seems to be a copy, since the 'tag' column shows only on b, not on a.
b
Out[70]:
1 2 tag
0 2 1 test
1 4 1 test
2 4 1 test
a
Out[71]:
0 1 2
0 1 2 1
1 2 4 1
2 3 4 1
c = a.loc[0:1, :]
c.loc[0, :] = 100 # why this is a view??
c
c seems to be a view of b. Because the changes on b is carried over to a.
Out[74]: c
0 1 2
0 100 100 100
1 2 4 1
a
Out[75]: a
0 1 2
0 100 100 100
1 2 4 1
2 3 4 1
Any why a.loc[:, 1:3] gives me 3 rows but a.loc[0:1, :] gives me 2 rows??
python pandas
add a comment |
up vote
3
down vote
favorite
I am trying to understand when do I get a copy vs view when using .loc method to index the dataframe. Getting a view to me means any changes on view will be carried over to the original df. I expect using .loc gives me a copy.
In below example, I am indexing the df using the same method, but one gives me a copy, the other one gives me a view.
Can someone point out what I am missing?
a = pd.DataFrame([[1,2, 1], [2, 4,1], [3,4,1]])
b = a.loc[:, 1:3]
b.loc[:, 'tag'] = 'test' # this is a copy?
b seems to be a copy, since the 'tag' column shows only on b, not on a.
b
Out[70]:
1 2 tag
0 2 1 test
1 4 1 test
2 4 1 test
a
Out[71]:
0 1 2
0 1 2 1
1 2 4 1
2 3 4 1
c = a.loc[0:1, :]
c.loc[0, :] = 100 # why this is a view??
c
c seems to be a view of b. Because the changes on b is carried over to a.
Out[74]: c
0 1 2
0 100 100 100
1 2 4 1
a
Out[75]: a
0 1 2
0 100 100 100
1 2 4 1
2 3 4 1
Any why a.loc[:, 1:3] gives me 3 rows but a.loc[0:1, :] gives me 2 rows??
python pandas
2
See pandas.pydata.org/pandas-docs/stable/… "Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the setitem will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about!"
– ayhan
Oct 17 at 17:18
Also github site github.com/pandas-dev/pandas/issues/6149
– W-B
Oct 17 at 17:21
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to understand when do I get a copy vs view when using .loc method to index the dataframe. Getting a view to me means any changes on view will be carried over to the original df. I expect using .loc gives me a copy.
In below example, I am indexing the df using the same method, but one gives me a copy, the other one gives me a view.
Can someone point out what I am missing?
a = pd.DataFrame([[1,2, 1], [2, 4,1], [3,4,1]])
b = a.loc[:, 1:3]
b.loc[:, 'tag'] = 'test' # this is a copy?
b seems to be a copy, since the 'tag' column shows only on b, not on a.
b
Out[70]:
1 2 tag
0 2 1 test
1 4 1 test
2 4 1 test
a
Out[71]:
0 1 2
0 1 2 1
1 2 4 1
2 3 4 1
c = a.loc[0:1, :]
c.loc[0, :] = 100 # why this is a view??
c
c seems to be a view of b. Because the changes on b is carried over to a.
Out[74]: c
0 1 2
0 100 100 100
1 2 4 1
a
Out[75]: a
0 1 2
0 100 100 100
1 2 4 1
2 3 4 1
Any why a.loc[:, 1:3] gives me 3 rows but a.loc[0:1, :] gives me 2 rows??
python pandas
I am trying to understand when do I get a copy vs view when using .loc method to index the dataframe. Getting a view to me means any changes on view will be carried over to the original df. I expect using .loc gives me a copy.
In below example, I am indexing the df using the same method, but one gives me a copy, the other one gives me a view.
Can someone point out what I am missing?
a = pd.DataFrame([[1,2, 1], [2, 4,1], [3,4,1]])
b = a.loc[:, 1:3]
b.loc[:, 'tag'] = 'test' # this is a copy?
b seems to be a copy, since the 'tag' column shows only on b, not on a.
b
Out[70]:
1 2 tag
0 2 1 test
1 4 1 test
2 4 1 test
a
Out[71]:
0 1 2
0 1 2 1
1 2 4 1
2 3 4 1
c = a.loc[0:1, :]
c.loc[0, :] = 100 # why this is a view??
c
c seems to be a view of b. Because the changes on b is carried over to a.
Out[74]: c
0 1 2
0 100 100 100
1 2 4 1
a
Out[75]: a
0 1 2
0 100 100 100
1 2 4 1
2 3 4 1
Any why a.loc[:, 1:3] gives me 3 rows but a.loc[0:1, :] gives me 2 rows??
python pandas
python pandas
edited Nov 20 at 4:21
asked Oct 17 at 17:10
Lisa
85311432
85311432
2
See pandas.pydata.org/pandas-docs/stable/… "Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the setitem will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about!"
– ayhan
Oct 17 at 17:18
Also github site github.com/pandas-dev/pandas/issues/6149
– W-B
Oct 17 at 17:21
add a comment |
2
See pandas.pydata.org/pandas-docs/stable/… "Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the setitem will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about!"
– ayhan
Oct 17 at 17:18
Also github site github.com/pandas-dev/pandas/issues/6149
– W-B
Oct 17 at 17:21
2
2
See pandas.pydata.org/pandas-docs/stable/… "Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the setitem will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about!"
– ayhan
Oct 17 at 17:18
See pandas.pydata.org/pandas-docs/stable/… "Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the setitem will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about!"
– ayhan
Oct 17 at 17:18
Also github site github.com/pandas-dev/pandas/issues/6149
– W-B
Oct 17 at 17:21
Also github site github.com/pandas-dev/pandas/issues/6149
– W-B
Oct 17 at 17:21
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52860237%2funderstand-a-value-is-trying-to-be-set-on-a-copy-of-a-slice-from-a-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
See pandas.pydata.org/pandas-docs/stable/… "Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the setitem will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about!"
– ayhan
Oct 17 at 17:18
Also github site github.com/pandas-dev/pandas/issues/6149
– W-B
Oct 17 at 17:21