First, let's do a simple experiment to see that r1
and r2
are actually the same objects in pandas
's sense
import pandas as pd
df = pd.DataFrame([0,1,2,3])
r1 = df.iloc[:,:1]
r2 = df.iloc[:,:1]
r1.iloc[2] = -10
r2.iloc[1] = -100
assert (not r1 is r2)
print(pd.concat((df,r1,r2),axis=1).to_string())
running this script, the output is
0 0 0
0 0 0 0
1 -100 -100 -100
2 -10 -10 -10
3 3 3 3
this means r1
and r2
are considered the same object by pandas
.
In fact, by running this script
unique_ids = []
for _ in range(1000):
one_id = id(df.iloc[:,:1])
unique_ids.append(one_id)
set(unique_ids)
you will see the length of set(unique_ids)
is not 1 !!
According to @user2357112 supports Monica's comment under this post
I don't think the ID you receive has any relation to the addresses of the array elements; it's the address of a header containing array metadata and a pointer to the storage used for the elements.
Basically, r1
and r2
are different objects referring to the same array elements.