From cd489e24d9532c2444c4c0fe3948e41ad4389cd7 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 5 Dec 2022 16:45:42 +0100 Subject: [PATCH 1/4] REGR: memory_usage showing unnecessary FutureWarning --- doc/source/whatsnew/v1.5.3.rst | 1 + pandas/core/frame.py | 1 + pandas/tests/io/formats/test_info.py | 10 ++++++++++ 3 files changed, 12 insertions(+) diff --git a/doc/source/whatsnew/v1.5.3.rst b/doc/source/whatsnew/v1.5.3.rst index b6c1c857717c7..8e5e94fd7d9a0 100644 --- a/doc/source/whatsnew/v1.5.3.rst +++ b/doc/source/whatsnew/v1.5.3.rst @@ -14,6 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed performance regression in :meth:`Series.isin` when ``values`` is empty (:issue:`49839`) +- Fixed regression in :meth:`DataFrame.memory_usage` showing unnecessary ``FutureWarning`` when :class:`DataFrame` is empty (:issue:`50066`) - Fixed regression in :meth:`DataFrameGroupBy.transform` when used with ``as_index=False`` (:issue:`49834`) - Enforced reversion of ``color`` as an alias for ``c`` and ``size`` as an alias for ``s`` in function :meth:`DataFrame.plot.scatter` (:issue:`49732`) - Fixed regression in :meth:`SeriesGroupBy.apply` setting a ``name`` attribute on the result if the result was a :class:`DataFrame` (:issue:`49907`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 98af277cc0bd7..51a59d061cc8f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3558,6 +3558,7 @@ def memory_usage(self, index: bool = True, deep: bool = False) -> Series: result = self._constructor_sliced( [c.memory_usage(index=False, deep=deep) for col, c in self.items()], index=self.columns, + dtype=np.int64, ) if index: index_memory_usage = self._constructor_sliced( diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index 33c78baa1eedc..11ccb4cb1d13e 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -20,6 +20,7 @@ date_range, option_context, ) +import pandas._testing as tm @pytest.fixture @@ -491,3 +492,12 @@ def test_info_int_columns(): """ ) assert result == expected + + +def test_memory_usage_empty_no_warning(): + # GH#50066 + df = DataFrame(index=["a", "b"]) + with tm.assert_produces_warning(None): + result = df.memory_usage() + expected = Series(16, index=["Index"]) + tm.assert_series_equal(result, expected) From 6bd399c093278069385f417e34d369bfaad04a5b Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 5 Dec 2022 19:48:13 +0100 Subject: [PATCH 2/4] Fix test --- pandas/tests/io/formats/test_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index 11ccb4cb1d13e..18f25a7b322b8 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -499,5 +499,5 @@ def test_memory_usage_empty_no_warning(): df = DataFrame(index=["a", "b"]) with tm.assert_produces_warning(None): result = df.memory_usage() - expected = Series(16, index=["Index"]) + expected = Series(16, index=["Index"], dtype="int64") tm.assert_series_equal(result, expected) From 11a61ac635f937169b3855fcc46f9c4caf03143c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 7 Dec 2022 20:34:38 +0100 Subject: [PATCH 3/4] Try intp --- pandas/core/frame.py | 2 +- pandas/tests/io/formats/test_info.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 51a59d061cc8f..8ca8a20e46f2f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3558,7 +3558,7 @@ def memory_usage(self, index: bool = True, deep: bool = False) -> Series: result = self._constructor_sliced( [c.memory_usage(index=False, deep=deep) for col, c in self.items()], index=self.columns, - dtype=np.int64, + dtype=np.intp, ) if index: index_memory_usage = self._constructor_sliced( diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index 18f25a7b322b8..11ccb4cb1d13e 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -499,5 +499,5 @@ def test_memory_usage_empty_no_warning(): df = DataFrame(index=["a", "b"]) with tm.assert_produces_warning(None): result = df.memory_usage() - expected = Series(16, index=["Index"], dtype="int64") + expected = Series(16, index=["Index"]) tm.assert_series_equal(result, expected) From 5c9b48e043951742ff88d03fb5b044845f6a6cc1 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 7 Dec 2022 22:44:10 +0100 Subject: [PATCH 4/4] Fix --- pandas/tests/io/formats/test_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index da2a9c5464722..06c1aacc3eac0 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -499,5 +499,5 @@ def test_memory_usage_empty_no_warning(): df = DataFrame(index=["a", "b"]) with tm.assert_produces_warning(None): result = df.memory_usage() - expected = Series(16, index=["Index"]) + expected = Series(16 if IS64 else 8, index=["Index"]) tm.assert_series_equal(result, expected)