w3resource

Pandas Series: memory_usage() function

Memory usage of Pandas Series

The memory_usage() function is used to get the memory usage of the Series.

The memory usage can optionally include the contribution of the index and of elements of object dtype.

Syntax:

Series.memory_usage(self, index=True, deep=False)
Pandas Series memory_usage() function

Parameters:

Name Description Type Default Value Required / Optional
index Specifies whether to include the memory usage of the Series index. bool True Required
deep If True, introspect the data deeply by interrogating object dtypes for system-level memory consumption, and include it in the returned value. bool False: 0 Optional

Returns: int - Bytes of memory consumed.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(range(4))
s.memory_usage()

Output:

112
Pandas Series memory_usage() function

Example - Not including the index gives the size of the rest of the data, which is necessarily smaller:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(range(4))
s.memory_usage(index=False)

Output:

32
Pandas Series memory_usage() function

Example - The memory footprint of object values is ignored by default:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(range(4))
s = pd.Series(["x", "y"])
s.values

Output:

array(['x', 'y'], dtype=object)

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(range(4))
s = pd.Series(["x", "y"])
s.memory_usage()

Output:

96
Pandas Series memory_usage() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(range(4))
s = pd.Series(["x", "y"])
s.memory_usage(deep=True)

Output:

204
Pandas Series memory_usage() function

Previous: Series as ndarray or ndarray-like in Pandas
Next: Change data type of a series in Pandas



Follow us on Facebook and Twitter for latest update.