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??










share|improve this question




















  • 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















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??










share|improve this question




















  • 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













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??










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen