When selecting data from a Pandas dataframe, sometimes a view is returned and sometimes a copy is returned. While there is a logic behind this, is there a way to force Pandas to explicitly return a view or a copy?
-
Can you provide a sample of the difference between a view and a copy? – cwharland May 06 '14 at 04:56
-
See here for the rules: http://stackoverflow.com/questions/23296282/what-rules-does-pandas-use-to-generate-a-view-vs-a-copy – Karl D. May 06 '14 at 04:59
-
@cwharland, my understanding is modifications to a view also modify the primary dataframe (so a reference), and a copy is... a copy. – calben May 06 '14 at 05:03
-
@KarlD., is that link the only way to handle the difference between views and copies? – calben May 06 '14 at 05:03
-
1I'm not sure you can as this is due to numpy and not pandas, the [docs](http://pandas-docs.github.io/pandas-docs-travis/indexing.html#indexing-view-versus-copy) show the various situations that should be avoided and due to the non-deterministic nature of the type of calls it is advised that chained assignment should be avoid and hence the warning, as I understand it only chained assignment is the access method that should be avoided – EdChum May 06 '14 at 07:26
-
This is not in general possible with a DataFrame without reaching into internals. It is possible to provide views at times, but as others point out is non-deterministic (e.g. when it happens), and is in general not very useful. Use the pandas indexers to set data instead. – Jeff May 06 '14 at 10:08
-
@Jeff Getting a view would be very useful indeed for modifying subsets. The idea is to use a view as a "window" to the data. I'm surprised there isn't a way to explicitly generate a view. If this is because of numpy then I'd propagate the blame to numpy. Just like a view would propagate an edit to the underlying dataframe. – Milind R Oct 04 '17 at 13:49
1 Answers
There are two parts to your question: (1) how to make a view (see bottom of this answer), and (2) how to make a copy.
I'll demonstrate with some example data:
import pandas as pd
df = pd.DataFrame([[1,2,3],[4,5,6],[None,10,20],[7,8,9]], columns=['x','y','z'])
# which looks like this:
x y z
0 1 2 3
1 4 5 6
2 NaN 10 20
3 7 8 9
How to make a copy: One option is to explicitly copy your DataFrame after whatever operations you perform. For instance, lets say we are selecting rows that do not have NaN:
df2 = df[~df['x'].isnull()]
df2 = df2.copy()
Then, if you modify values in df2 you will find that the modifications do not propagate back to the original data (df), and that Pandas does not warn that "A value is trying to be set on a copy of a slice from a DataFrame"
df2['x'] *= 100
# original data unchanged
print(df)
x y z
0 1 2 3
1 4 5 6
2 NaN 10 20
3 7 8 9
# modified data
print(df2)
x y z
0 100 2 3
1 400 5 6
3 700 8 9
Note: you may take a performance hit by explicitly making a copy.
How to ignore warnings: Alternatively, in some cases you might not care whether a view or copy is returned, because your intention is to permanently modify the data and never go back to the original data. In this case, you can suppress the warning and go merrily on your way (just don't forget that you've turned it off, and that the original data may or may not be modified by your code, because df2 may or may not be a copy):
pd.options.mode.chained_assignment = None # default='warn'
For more information, see the answers at How to deal with SettingWithCopyWarning in Pandas?
How to make a view: Pandas will implicitly make views wherever and whenever possible. The key to this is to use the df.loc[row_indexer,col_indexer]
method. For example, to multiply the values of column y
by 100 for only the rows where column x
is not null, we would write:
mask = ~df['x'].isnull()
df.loc[mask, 'y'] *= 100
# original data has changed
print(df)
x y z
0 1.0 200 3
1 4.0 500 6
2 NaN 10 20
3 7.0 800 9

- 581
- 1
- 7
- 19
-
1
-
This answer, frustratingly, answers the opposite of the question asked, just like the rest of the internet. -1 – Milind R Oct 04 '17 at 13:46
-
1The question was "is there a way to force Pandas to explicitly return a view or a copy?" to which I answered how to explicitly make a copy. I suppose the frustrating part is if you explicitly wanted a view instead? Also note that the question title differs from the question in description...which doesn't help. – MD004 Oct 05 '17 at 17:51
-
-
-
In your last example, what if I need to separate the mask query from the multiplication by a few lines of code: `view = df.loc[mask]` followed by a few lines of code, and then `view.y *= 100)`. The view isn't really a view, so this doesn't work. Can I force that query to return a view that I can hold onto? – bigh_29 Mar 21 '20 at 16:11
-
Right, in that case you get "a copy of a slice from a DataFrame" rather than a view. I'm not sure if you can force it to give you a view. I would suggest storing the `mask` for later rather than `view`; i.e. pass along `mask` and `df` to your later lines of code so that you can still write `df.loc[mask, 'y']` – MD004 Mar 25 '20 at 23:59