Pandas Series.memory_usage


Pandas Series.memory_usage() function

In this tutorial, we'll go through examples for the Pandas Series.memory_usage() function, which returns the memory usage of the Series.

The memory usage also reflects the contribution of the index and of elements of the Series object.

The syntax of Series.memory_usage() function is

Series.memory_usage(index=True, deep=False)

where

ParameterDescription
index[Optional] Boolean flag that specifies whether to include the memory usage of the Series index.
deep[Optional] Boolean flag. If True, introspect the data deeply by interrogating object
 dtypes for system-level memory consumption, and include it in the returned value.

Examples for Series.memory_usage property

1. Getting memory usage of given Pandas Series with index

In the following program, we take a Series object in series_data, and get its memory usage including the index of the Series.

The default value of index parameter in Series.memory_usage() is True. Therefore, if we do not specify any argument for index parameter, memory_usage() considers the storage occupied for the index as well.

Python Program

import pandas as pd

# Create a Pandas Series
series_data = pd.Series([10, 20, 30, 40, 50])

# Find memory usage
result = series_data.memory_usage()

# Display the result
print(f"Memory usage\n{result}")

Output

Memory usage
172

2. Getting memory usage of given Pandas Series without index

In the following program, we take a Series object in series_data, and get its memory usage without including the index of the Series.

Pass False as argument for the index parameter in Series.memory_usage().

Python Program

import pandas as pd

# Create a Pandas Series
series_data = pd.Series([10, 20, 30, 40, 50])

# Find memory usage
result = series_data.memory_usage(index=False)

# Display the result
print(f"Memory usage\n{result}")

Output

Memory usage
40


Python Libraries