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)
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
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
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
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
Previous: Series as ndarray or ndarray-like in Pandas
Next: Change data type of a series in Pandas
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-memory_usage.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics