Skip to content

Commit 805d765

Browse files
BUG: Fix getsizeof when using Series(obj) and taking into account GC corrections (#52112)
* BUG: Fix getsizeof when using Series(obj) and taking into account GC corrections Calling __sizeof__ to compute the size of an object produces an error, and in general, this method does not take Garbage Collector corrections into account. * BUG: Fix getsizeof when using Series(obj) and taking into account GC corrections Calling __sizeof__ to compute the size of an object produces an error, and in general, this method does not take Garbage Collector corrections into account. * Removing unnecessary tests that depend on OS bits * TST: Included python data types * TST: Added more objects Series with objects for Memory test * TST: Added more objects Series with objects for Memory test * CLN: Deleted unnecesary tests * TYP: Renamed _object_series to _type_objects_series
1 parent 25c1942 commit 805d765

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ Styler
257257
Other
258258
^^^^^
259259
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
260+
- Bug in :meth:`Series.memory_usage` when ``deep=True`` throw an error with Series of objects and the returned value is incorrect, as it does not take into account GC corrections (:issue:`51858`)
260261

261262
.. ***DO NOT USE THIS SECTION***
262263

pandas/_libs/lib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import abc
22
from decimal import Decimal
33
from enum import Enum
4+
from sys import getsizeof
45
from typing import (
56
Literal,
67
_GenericAlias,
@@ -159,7 +160,7 @@ def memory_usage_of_objects(arr: object[:]) -> int64_t:
159160

160161
n = len(arr)
161162
for i in range(n):
162-
size += arr[i].__sizeof__()
163+
size += getsizeof(arr[i])
163164
return size
164165

165166

pandas/_testing/__init__.py

+17
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@
177177
np.uint32,
178178
]
179179

180+
PYTHON_DATA_TYPES = [
181+
str,
182+
int,
183+
float,
184+
complex,
185+
list,
186+
tuple,
187+
range,
188+
dict,
189+
set,
190+
frozenset,
191+
bool,
192+
bytes,
193+
bytearray,
194+
memoryview,
195+
]
196+
180197
ENDIAN = {"little": "<", "big": ">"}[byteorder]
181198

182199
NULL_OBJECTS = [None, np.nan, pd.NaT, float("nan"), pd.NA, Decimal("NaN")]

pandas/conftest.py

+23
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,29 @@ def index_or_series_obj(request):
760760
return _index_or_series_objs[request.param].copy(deep=True)
761761

762762

763+
_typ_objects_series = {
764+
f"{dtype.__name__}-series": Series(dtype) for dtype in tm.PYTHON_DATA_TYPES
765+
}
766+
767+
768+
_index_or_series_memory_objs = {
769+
**indices_dict,
770+
**_series,
771+
**_narrow_series,
772+
**_typ_objects_series,
773+
}
774+
775+
776+
@pytest.fixture(params=_index_or_series_memory_objs.keys())
777+
def index_or_series_memory_obj(request):
778+
"""
779+
Fixture for tests on indexes, series, series with a narrow dtype and
780+
series with empty objects type
781+
copy to avoid mutation, e.g. setting .name
782+
"""
783+
return _index_or_series_memory_objs[request.param].copy(deep=True)
784+
785+
763786
# ----------------------------------------------------------------
764787
# DataFrames
765788
# ----------------------------------------------------------------

pandas/tests/base/test_misc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def test_ndarray_compat_properties(index_or_series_obj):
8282

8383

8484
@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
85-
def test_memory_usage(index_or_series_obj):
86-
obj = index_or_series_obj
85+
def test_memory_usage(index_or_series_memory_obj):
86+
obj = index_or_series_memory_obj
8787
# Clear index caches so that len(obj) == 0 report 0 memory usage
8888
if isinstance(obj, Series):
8989
is_ser = True

0 commit comments

Comments
 (0)