Skip to content
Prev Previous commit
Next Next commit
Define memory_usage on SparseArray
  • Loading branch information
hexgnu committed Feb 5, 2018
commit 207bc74d2e4825102b88bde86cecdd11c589adb4
8 changes: 4 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,10 +1070,10 @@ def memory_usage(self, deep=False):
numpy.ndarray.nbytes
"""
# Use sparse values if they exist for memory consumption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather override this in SparseSeries/SparseArray

if hasattr(self.values, 'sp_values'):
values = self.values.sp_values
else:
values = self.values
# if hasattr(self.values, 'sp_values'):
# values = self.values.sp_values
# else:
values = self.values

if hasattr(values, 'memory_usage'):
return values.memory_usage(deep=deep)
Expand Down
38 changes: 37 additions & 1 deletion pandas/core/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pandas.core.base import PandasObject

from pandas import compat
from pandas.compat import range
from pandas.compat import range, PYPY
from pandas.compat.numpy import function as nv

from pandas.core.dtypes.generic import ABCSparseSeries
Expand All @@ -30,6 +30,7 @@
from pandas.core.dtypes.missing import isna, notna, na_value_for_dtype

import pandas._libs.sparse as splib
import pandas._libs.lib as lib
from pandas._libs.sparse import SparseIndex, BlockIndex, IntIndex
from pandas._libs import index as libindex
import pandas.core.algorithms as algos
Expand Down Expand Up @@ -238,6 +239,41 @@ def kind(self):
elif isinstance(self.sp_index, IntIndex):
return 'integer'

def memory_usage(self, deep=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make the doc-string generic in base and use a shared one here?

"""Memory usage of SparseArray

Parameters
----------
deep : bool
Introspect the data deeply, interrogate
`object` dtypes for system-level memory consumption

Returns
-------
scalar bytes of memory consumed

Notes
-----
Memory usage does not include memory of empty cells filled by
fill_value. And it does not include memory consumed by
elements that are not components of the array if deep=False

See also
--------
Series.memory_usage
"""

values = self.sp_values
if hasattr(values, 'memory_usage'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this here as the values are always an ndarray

return values.memory_usage(deep=deep)

v = values.nbytes

if deep and is_object_dtype(self) and not PYPY:
v += lib.memory_usage_of_objects(values)

return v

def __array_wrap__(self, out_arr, context=None):
"""
NumPy calls this method when ufunc is applied
Expand Down