Skip to content

Commit de8ca78

Browse files
authored
BUG: fix memory_usage method with deep of StringArray (#33985)
1 parent 07b9f7a commit de8ca78

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ ExtensionArray
758758
- Fixed bug where :meth:`Series.value_counts` would raise on empty input of ``Int64`` dtype (:issue:`33317`)
759759
- Fixed bug in :class:`Series` construction with EA dtype and index but no data or scalar data fails (:issue:`26469`)
760760
- Fixed bug that caused :meth:`Series.__repr__()` to crash for extension types whose elements are multidimensional arrays (:issue:`33770`).
761+
- Fixed bug where :meth:`StringArray.memory_usage` was not implemented (:issue:`33963`)
761762

762763

763764
Other

pandas/core/arrays/string_.py

+6
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ def value_counts(self, dropna=False):
292292

293293
return value_counts(self._ndarray, dropna=dropna).astype("Int64")
294294

295+
def memory_usage(self, deep=False):
296+
result = self._ndarray.nbytes
297+
if deep:
298+
return result + lib.memory_usage_of_objects(self._ndarray)
299+
return result
300+
295301
# Override parent because we have different return types.
296302
@classmethod
297303
def _create_arithmetic_method(cls, op):

pandas/tests/arrays/string_/test_string.py

+7
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,10 @@ def test_value_counts_na():
303303
result = arr.value_counts(dropna=True)
304304
expected = pd.Series([2, 1], index=["a", "b"], dtype="Int64")
305305
tm.assert_series_equal(result, expected)
306+
307+
308+
def test_memory_usage():
309+
# GH 33963
310+
series = pd.Series(["a", "b", "c"], dtype="string")
311+
312+
assert 0 < series.nbytes <= series.memory_usage() < series.memory_usage(deep=True)

0 commit comments

Comments
 (0)