使用pandas inplace关键字参数的准则 [英] guidelines on using pandas inplace keyword argument

查看:64
本文介绍了使用pandas inplace关键字参数的准则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用inplace的准则是什么?

例如

df = df.reset_index()

df.reset_index(inplace=True)

相同但不同吗?

推荐答案

就生成的DataFrame df而言,两种方法是相同的.区别在于(最大)内存使用情况,因为就地版本不会创建DataFrame的副本.

In terms of the resulting DataFrame df, the two approaches are the same. The difference lies in the (maximum) memory usage, since the in-place version does not create a copy of the DataFrame.

请考虑以下设置:

import numpy as np
import pandas as pd

def make_data():
    return pd.DataFrame(np.random.rand(1000000, 100))

def func_copy():
    df = make_data()
    df = df.reset_index()

def func_inplace():
    df = make_data()
    df.reset_index(inplace=True)

我们可以使用memory_profile库对内存使用情况进行一些基准测试:

We can use the memory_profile library to perform some benchmarking for the memory usage:

%load_ext memory_profiler

%memit func_copy()
# peak memory: 1602.66 MiB, increment: 1548.66 MiB

%memit func_inplace()
# peak memory: 817.02 MiB, increment: 762.94 MiB

如预期的那样,就地版本具有更高的内存效率.

As expected, the in-place version is more memory efficient.

另一方面,当数据大小足够大时(例如,在上面的示例中),两种方法之间的运行时间似乎也存在不小的差异:

On the other hand, there also seems to be a non-trivial difference in running time between the approaches when the data size is large enough (e.g. in the above example):

%timeit func_copy()
1 loops, best of 3: 2.56 s per loop

%timeit func_inplace()
1 loops, best of 3: 1.35 s per loop

根据使用情况(例如临时探索性分析与生产代码),数据大小和可用的硬件资源,这些差异可能会也可能不会很明显.通常,最好使用就地版本,以提高内存和运行时间效率.

These differences may or may not be significant depending on the use case (e.g. adhoc exploratory analysis vs. production code), data size and the hardware resource available. In general, it might be a good idea to use the in-place version whenever possible for better memory and run time efficiency.

这篇关于使用pandas inplace关键字参数的准则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆