bff.mem_usage_pd

bff.mem_usage_pd(pd_obj, index=True, deep=True, details=True)

Calculate the memory usage of a pandas object.

If details, returns a dictionary with the memory usage and type of each column (DataFrames only). Key=column, value=(memory, type). Else returns a dictionary with the total memory usage. Key=`total`, value=memory.

Parameters
  • pd_obj (pd.DataFrame or pd.Series) – DataFrame or Series to calculate the memory usage.

  • index (bool, default True) – If True, include the memory usage of the index.

  • deep (bool, default True) – If True, introspect the data deeply by interrogating object dtypes for system-level memory consumption.

  • details (bool, default True) – If True and a DataFrame is given, give the detail (memory and type) of each column.

Returns

Dictionary with the column or total as key and the memory usage as value (with ‘MB’).

Return type

dict of str to str

Raises

AttributeError – If argument is not a pandas object.

Examples

>>> import pandas as pd
>>> df = pd.DataFrame({'A': [f'value{i}' for i in range(100_000)],
...                    'B': [i for i in range(100_000)],
...                    'C': [float(i) for i in range(100_000)]}).set_index('A')
>>> mem_usage_pd(df)
{'total': '7.90 MB'}
>>> mem_usage_pd(df, details=True)
{'Index': {'6.38 MB', 'Index type'},
 'B': {'0.76 MB', dtype('int64')},
 'C': {'0.76 MB', dtype('float64')},
 'total': '7.90 MB'}
>>> serie = df.reset_index()['B']
>>> mem_usage_pd(serie)
{'total': '0.76 MB'}
>>> mem_usage_pd(serie, details=True)
2019-06-24 11:23:39,500 Details is only available for DataFrames.
{'total': '0.76 MB'}