为什么对Pandas DataFrame进行深度复制不会影响内存使用?

Max*_*gal 6 python pandas

我有以下代码:

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应该克隆对象并因此增加内存使用量?

Rig*_*leg 4

当调用copy.deepcopy一个对象时foo__dict__会查找它__deepcopy__的方法,然后依次调用该方法。对于实例DataFrame,它扩展了NDFrame类,该类的__deepcopy__方法将操作委托给NDFrame.copy.

这是以下的实现NDFrame.__deepcopy__

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 列表,因此实际上并未复制。

  • @9769953 我明白了,它是某种写入副本。我无法解释确切的机制,但是“deepcopy”的结果是浅拷贝(这是必要的,因为它很快并且不会增加内存),然后写入该副本确实会增加消耗的内存。仅用一种文字是看不出来的,我无法解释这种增强的模式,但肯定是这样的。 (3认同)
  • @InyoungKim 据我了解,索引是深度复制,但数据不是。 (2认同)