我有以下代码:
import resource
from copy import deepcopy
import pandas as pd
import random
n=100000
df = pd.DataFrame({'x': [random.randint(1, 1000) for i in range(n)],
'y': [random.randint(1, 1000) for i in range(n)],
'z': [random.randint(1, 1000) for i in range(n)]
})
df2=deepcopy(df)
print 'memory peak: {kb} kb'.format(kb=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
Run Code Online (Sandbox Code Playgroud)
我希望在df2=deepcopy(df)
没有代码的情况下,此代码会看到不同的内存峰值用法。但是内存显示的结果完全相同。是否不deepcopy
应该克隆对象并因此增加内存使用量?
当调用copy.deepcopy
一个对象时foo
,__dict__
会查找它__deepcopy__
的方法,然后依次调用该方法。对于实例DataFrame
,它扩展了NDFrame
类,该类的__deepcopy__
方法将操作委托给NDFrame.copy
.
def __deepcopy__(self, memo=None):
"""
Parameters
----------
memo, default None
Standard signature. Unused
"""
if memo is None:
memo = {}
return self.copy(deep=True)
Run Code Online (Sandbox Code Playgroud)
您可以在此处NDFrame.copy
阅读的文档字符串,但以下是主要摘录:
def copy(self, deep=True):
"""
Make a copy of this object's indices and data.
When ``deep=True`` (default), a new object will be created with a
copy of the calling object's data and indices. Modifications to
the data or indices of the copy will not be reflected in the
original object (see notes below).
(...)
Notes
-----
When ``deep=True``, data is copied but actual Python objects
will not be copied recursively, only the reference to the object.
This is in contrast to `copy.deepcopy` in the Standard Library,
which recursively copies object data (see examples below).
Run Code Online (Sandbox Code Playgroud)
在您的示例中,数据是 Python 列表,因此实际上并未复制。
归档时间: |
|
查看次数: |
225 次 |
最近记录: |