|
11 | 11 | from pandas.core.base import PandasObject
|
12 | 12 |
|
13 | 13 | from pandas import compat
|
14 |
| -from pandas.compat import range |
| 14 | +from pandas.compat import range, PYPY |
15 | 15 | from pandas.compat.numpy import function as nv
|
16 | 16 |
|
17 | 17 | from pandas.core.dtypes.generic import ABCSparseSeries
|
|
30 | 30 | from pandas.core.dtypes.missing import isna, notna, na_value_for_dtype
|
31 | 31 |
|
32 | 32 | import pandas._libs.sparse as splib
|
| 33 | +import pandas._libs.lib as lib |
33 | 34 | from pandas._libs.sparse import SparseIndex, BlockIndex, IntIndex
|
34 | 35 | from pandas._libs import index as libindex
|
35 | 36 | import pandas.core.algorithms as algos
|
@@ -238,6 +239,41 @@ def kind(self):
|
238 | 239 | elif isinstance(self.sp_index, IntIndex):
|
239 | 240 | return 'integer'
|
240 | 241 |
|
| 242 | + def memory_usage(self, deep=False): |
| 243 | + """Memory usage of SparseArray |
| 244 | +
|
| 245 | + Parameters |
| 246 | + ---------- |
| 247 | + deep : bool |
| 248 | + Introspect the data deeply, interrogate |
| 249 | + `object` dtypes for system-level memory consumption |
| 250 | +
|
| 251 | + Returns |
| 252 | + ------- |
| 253 | + scalar bytes of memory consumed |
| 254 | +
|
| 255 | + Notes |
| 256 | + ----- |
| 257 | + Memory usage does not include memory of empty cells filled by |
| 258 | + fill_value. And it does not include memory consumed by |
| 259 | + elements that are not components of the array if deep=False |
| 260 | +
|
| 261 | + See also |
| 262 | + -------- |
| 263 | + Series.memory_usage |
| 264 | + """ |
| 265 | + |
| 266 | + values = self.sp_values |
| 267 | + if hasattr(values, 'memory_usage'): |
| 268 | + return values.memory_usage(deep=deep) |
| 269 | + |
| 270 | + v = values.nbytes |
| 271 | + |
| 272 | + if deep and is_object_dtype(self) and not PYPY: |
| 273 | + v += lib.memory_usage_of_objects(values) |
| 274 | + |
| 275 | + return v |
| 276 | + |
241 | 277 | def __array_wrap__(self, out_arr, context=None):
|
242 | 278 | """
|
243 | 279 | NumPy calls this method when ufunc is applied
|
|
0 commit comments