diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index c3396a73f62ed..816ef4e5c9eb6 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -758,6 +758,7 @@ ExtensionArray - Fixed bug where :meth:`Series.value_counts` would raise on empty input of ``Int64`` dtype (:issue:`33317`) - Fixed bug in :class:`Series` construction with EA dtype and index but no data or scalar data fails (:issue:`26469`) - Fixed bug that caused :meth:`Series.__repr__()` to crash for extension types whose elements are multidimensional arrays (:issue:`33770`). +- Fixed bug where :meth:`StringArray.memory_usage` was not implemented (:issue:`33963`) Other diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index 51bbe182a002b..537b1cf3dd439 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -292,6 +292,12 @@ def value_counts(self, dropna=False): return value_counts(self._ndarray, dropna=dropna).astype("Int64") + def memory_usage(self, deep=False): + result = self._ndarray.nbytes + if deep: + return result + lib.memory_usage_of_objects(self._ndarray) + return result + # Override parent because we have different return types. @classmethod def _create_arithmetic_method(cls, op): diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index eb89798a1ad96..b681abf03a2b3 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -303,3 +303,10 @@ def test_value_counts_na(): result = arr.value_counts(dropna=True) expected = pd.Series([2, 1], index=["a", "b"], dtype="Int64") tm.assert_series_equal(result, expected) + + +def test_memory_usage(): + # GH 33963 + series = pd.Series(["a", "b", "c"], dtype="string") + + assert 0 < series.nbytes <= series.memory_usage() < series.memory_usage(deep=True)