Skip to content

Instantly share code, notes, and snippets.

@dusenberrymw
Created April 14, 2017 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dusenberrymw/358b2e013918162c88bbc4ce2ca9b746 to your computer and use it in GitHub Desktop.
Save dusenberrymw/358b2e013918162c88bbc4ce2ca9b746 to your computer and use it in GitHub Desktop.
NumPy memory address experiments
# NumPy memory experiments
import numpy as np
a = np.random.rand(1, 2, 3)
b = np.asarray(a) # no copy!
c = np.array(a) # copy!
# The array interface is a map that has a 'data' key returning
# a tuple containing a pointer to the memory address and a
# return flag.
print(hex(a.__array_interface__['data'][0]))
print(hex(b.__array_interface__['data'][0])) # no copy!
print(hex(c.__array_interface__['data'][0])) # copy!
# ---
import numpy as np
import pandas as pd
a = np.random.rand(10,20)
df = pd.DataFrame(a)
b = np.asarray(df) # no copy!
c = df.values # no copy!
d = np.array(df) # copy!
print(hex(a.__array_interface__['data'][0]))
print(hex(b.__array_interface__['data'][0])) # no copy!
print(hex(c.__array_interface__['data'][0])) # copy!
print(hex(d.__array_interface__['data'][0])) # copy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment