pandas.DataFrame.to_numpy
方法有一个copy
参数,具有以下文档:
copy : bool,默认为False
是否确保返回的值不是另一个数组的视图。请注意,copy=False不能确保to_numpy()不会复制。相反,copy=True确保即使不是严格必要的也会进行复制。
稍微尝试一下,似乎在对内存相邻且没有混合类型的数据调用to_numpy
时,会保留视图。但是如何检查生成的numpy数组是否与创建它的数据帧共享内存,而不更改数据?
内存共享示例:
import pandas as pd
import numpy as np
# some data frame that I expect not to be copied
frame = pd.DataFrame(np.arange(144).reshape(12,12))
array = frame.to_numpy()
array[:] = 0
print(frame)
# Prints:
# 0 1 2 3 4 5 6 7 8 9 10 11
# 0 0 0 0 0 0 0 0 0 0 0 0 0
# 1 0 0 0 0 0 0 0 0 0 0 0 0
# 2 0 0 0 0 0 0 0 0 0 0 0 0
# 3 0 0 0 0 0 0 0 0 0 0 0 0
# 4 0 0 0 0 0 0 0 0 0 0 0 0
# 5 0 0 0 0 0 0 0 0 0 0 0 0
# 6 0 0 0 0 0 0 0 0 0 0 0 0
# 7 0 0 0 0 0 0 0 0 0 0 0 0
# 8 0 0 0 0 0 0 0 0 0 0 0 0
# 9 0 0 0 0 0 0 0 0 0 0 0 0
# 10 0 0 0 0 0 0 0 0 0 0 0 0
# 11 0 0 0 0 0 0 0 0 0 0 0 0
示例不共享内存:
import pandas as pd
import numpy as np
# some data frame that I expect to be copied
types = [int, str, float]
frame = pd.DataFrame({
i: [types[i%len(types)](value) for value in col]
for i, col in enumerate(np.arange(144).reshape(12,12).T)
})
array = frame.to_numpy()
array[:] = 0
print(frame)
# Prints:
# 0 1 2 3 4 5 6 7 8 9 10 11
# 0 0 12 24.0 36 48 60.0 72 84 96.0 108 120 132.0
# 1 1 13 25.0 37 49 61.0 73 85 97.0 109 121 133.0
# 2 2 14 26.0 38 50 62.0 74 86 98.0 110 122 134.0
# 3 3 15 27.0 39 51 63.0 75 87 99.0 111 123 135.0
# 4 4 16 28.0 40 52 64.0 76 88 100.0 112 124 136.0
# 5 5 17 29.0 41 53 65.0 77 89 101.0 113 125 137.0
# 6 6 18 30.0 42 54 66.0 78 90 102.0 114 126 138.0
# 7 7 19 31.0 43 55 67.0 79 91 103.0 115 127 139.0
# 8 8 20 32.0 44 56 68.0 80 92 104.0 116 128 140.0
# 9 9 21 33.0 45 57 69.0 81 93 105.0 117 129 141.0
# 10 10 22 34.0 46 58 70.0 82 94 106.0 118 130 142.0
# 11 11 23 35.0 47 59 71.0 83 95 107.0 119 131 143.0
np.shares_memory
。它不够快,但是np.may_share_memory
就像是copy
参数一样,可以保证两个数组不共享数据,但只能暗示它们是否共享。 - Martin