mars.dataframe.DataFrame.memory_usage#

DataFrame.memory_usage(index=True, deep=False)#

返回每列的内存使用情况(以字节为单位)。

内存使用情况可以选择性地包括索引和object数据类型的元素的贡献。

此值在DataFrame.info中默认显示。通过将pandas.options.display.memory_usage设置为 False,可以抑制此显示。

Parameters
  • index (bool, 默认值为 True) – 指定是否在返回的 Series 中包含 DataFrame 索引的内存使用情况。如果 index=True,索引的内存使用情况将是输出中的第一个项。

  • 深度 (布尔值, 默认为 False) – 如果为 True,通过询问对象的数据类型来深入检查数据的系统级内存消耗,并将其包含在返回的值中。

Returns

一个索引为原始列名的系列,其值为每列的内存使用量(以字节为单位)。

Return type

系列

另请参阅

numpy.ndarray.nbytes

ndarray元素消耗的总字节数。

Series.memory_usage

系列消耗的字节数。

Categorical

用于字符串值且有许多重复值的内存高效数组。

DataFrame.info

数据框的简明总结。

示例

>>> import mars.tensor as mt
>>> import mars.dataframe as md
>>> dtypes = ['int64', 'float64', 'complex128', 'object', 'bool']
>>> data = dict([(t, mt.ones(shape=5000).astype(t))
...              for t in dtypes])
>>> df = md.DataFrame(data)
>>> df.head().execute()
   int64  float64            complex128  object  bool
0      1      1.0    1.000000+0.000000j       1  True
1      1      1.0    1.000000+0.000000j       1  True
2      1      1.0    1.000000+0.000000j       1  True
3      1      1.0    1.000000+0.000000j       1  True
4      1      1.0    1.000000+0.000000j       1  True
>>> df.memory_usage().execute()
Index           128
int64         40000
float64       40000
complex128    80000
object        40000
bool           5000
dtype: int64
>>> df.memory_usage(index=False).execute()
int64         40000
float64       40000
complex128    80000
object        40000
bool           5000
dtype: int64

object 数据类型列的内存占用默认情况下被忽略:

>>> df.memory_usage(deep=True).execute()
Index            128
int64          40000
float64        40000
complex128     80000
object        160000
bool            5000
dtype: int64

使用分类类型高效存储具有许多重复值的对象数据类型列。

>>> df['object'].astype('category').memory_usage(deep=True).execute()
5216