Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two variables refers to the same pandas object?

Tags:

python

pandas

In this example, r1 and r2 refers to the same object but assert(not r1 is r2) returns false because their id are different. However, I would expect it to fail since r1 and r2 refers to the same object!!

import pandas as pd
df = pd.DataFrame([0])
r1 = df.iloc[0]
r2 = df.iloc[0]
assert(not r1 is r2)
r1[0] = 1
assert(r1.equals(r2))
print(id(r1), id(r2))
>> 140547055257416 140547055258032

Explanations on why this happens can be found in array slicing in numpy

like image 971
Chuan Avatar asked Oct 27 '25 04:10

Chuan


2 Answers

You can use np.may_share_memory or np.shares_memory here

np.may_share_memory(r1, r2)
# True
like image 129
Ch3steR Avatar answered Oct 30 '25 02:10

Ch3steR


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.

like image 32
meTchaikovsky Avatar answered Oct 30 '25 01:10

meTchaikovsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!