2

I don't understand why python's id() function returns different values for an element in a pandas' dataframe

The dataframe looks like this:

>>>df
   A  B
0  1  2
1  4  5
2  3  6

And the result of the function is the following:

>>> id(df.loc[0].A)
140079556632464
>>> id(df.loc[0].A)
140079556632440
>>> id(df.loc[0].A)
140079556632416
>>> id(df.loc[0].A)
140079556632464
>>> id(df.loc[0].A)
140079556632440
>>> id(df.loc[0].A)
140079556632416

Shouldn't the function return always the same value? Is this related to some internal implementation of pandas or is it fundamental to python?

gintro
  • 21
  • 1
  • Why should it return the same value? `loc` returns a copy of the data, so the ID will differ each time. If you are seeing recurring IDs in patterns, that means python is cacheing the ID values. – cs95 Jan 06 '19 at 17:50
  • I don't think so: `.loc` can return a view or a copy, cf. https://stackoverflow.com/questions/23296282/what-rules-does-pandas-use-to-generate-a-view-vs-a-copy#23296545 – gintro Jan 07 '19 at 18:43

0 Answers0