Is there an easy way to check whether two data frames are different copies or views of the same underlying data that doesn't involve manipulations? I'm trying to get a grip on when each is generated, and given how idiosyncratic the rules seem to be, I'd like an easy way to test.
For example, I thought "id(df.values)" would be stable across views, but they don't seem to be:
# Make two data frames that are views of same data.
df = pd.DataFrame([[1,2,3,4],[5,6,7,8]], index = ['row1','row2'],
columns = ['a','b','c','d'])
df2 = df.iloc[0:2,:]
# Demonstrate they are views:
df.iloc[0,0] = 99
df2.iloc[0,0]
Out[70]: 99
# Now try and compare the id on values attribute
# Different despite being views!
id(df.values)
Out[71]: 4753564496
id(df2.values)
Out[72]: 4753603728
# And we can of course compare df and df2
df is df2
Out[73]: False
Other answers I've looked up that try to give rules, but don't seem consistent, and also don't answer this question of how to test:
And of course: - http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy
UPDATE: Comments below seem to answer the question -- looking at the df.values.base
attribute rather than df.values
attribute does it, as does a reference to the df._is_copy
attribute (though the latter is probably very bad form since it's an internal).
df2._is_view
returnsTrue
but given that it's marked as private/internal, there may be a better way to go about it. – Jensdf2.values.base is df.values.base
– Snatchydf.values
will create a copy, unless its a single dtype (as from being computationally expensive). Why do you care if its a view and what are you actually trying to do? – Trepangid's
? Why they are different if there is only one object? Or view is another object? – Redpencil