How to find out `DataFrame.to_numpy` did not create a copy
There is numpy.shares_memory you can use:
# Your first example
print(np.shares_memory(array, frame)) # True, they are sharing memory
# Your second example
print(np.shares_memory(array2, frame2)) # False, they are not sharing memory
There is also numpy.may_share_memory, which is faster but can only be used for making sure things do not share memory (because it only checks whether the bounds overlap), so strictly speaking does not answer the question. Read this for the differences.
Take care using these numpy functions with pandas data-structures:
np.shares_memory(frame, frame)
returns True
for the first example, but False
for the second, probably because the __array__
method of the data frame in the second example creates a copy behind the scenes.