Please help me to understand: what is a view
in Pandas.
I know that if we change something in a view
we always make changes in the original object.
But a view of an object and the original object have different id's
for example. Does it mean that the view
is another object with reference to original object? What is the mechanism?
I tried but can't find an explanation.
import pandas as pd
import numpy as np
df = pd.DataFrame({'x': [1,2]})
print(df)
df_sub = df[0:1]
df_sub.x = -1
print(df_sub._is_view) # True
print(id(df) == id(df_sub)) # False
print(np.shares_memory(df, df_sub)) # True
To put it very simply, a view is a subset of the original object ( DataFrame or Series ) linked to the original source, while a copy is an entirely new object .
loc[mask] returns a new DataFrame with a copy of the data from df . Then df.
To understand what a View is, you have to know what an arrays is. An array is not only the "stuff" (items) you put in it. It needs (besides others) also information about the number of elements, the shape of your array and how to interpret the elements.
So an array would be an object at least containing these attributes:
class Series:
data # A pointer to where your array is stored
size # The number of items in your array
shape # The shape of your array
dtype # How to interpret the array
So when you create a view a new array object is created but (and that's important) the View's data
pointer points to the original array. It could be offset but it still points to one memory location that belongs to the original array. But even though it shares some data with the original the size, shape, dtype (, ...) might have changed so it requires a new object. That's why they have different id
s.
Think of it like windows. You have a garden (the array) and you have several windows, each window is a different object but all of them look out at the same (your) garden. Ok, granted, with some slicing operations you would have more escher-like windows but a metaphor always lacks some details :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With