From ef5d8f6ea1d09d112a5cafe2d8795ac628b66da9 Mon Sep 17 00:00:00 2001 From: Shao Yang Hong Date: Sun, 22 Nov 2020 18:11:45 +0800 Subject: [PATCH 1/5] DOC: Deprecate null_counts --- pandas/core/frame.py | 10 +++++++++- pandas/io/formats/info.py | 5 ++++- pandas/tests/io/formats/test_format.py | 4 ++-- pandas/tests/io/formats/test_info.py | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 27713b5bde201..9893e2608ee53 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2629,8 +2629,16 @@ def info( buf: Optional[IO[str]] = None, max_cols: Optional[int] = None, memory_usage: Optional[Union[bool, str]] = None, + show_counts: Optional[bool] = None, null_counts: Optional[bool] = None, ) -> None: + if null_counts is not None and show_counts is None: + warnings.warn( + "null_counts is deprecated. Use show_counts instead", + FutureWarning, + stacklevel=2, + ) + show_counts = null_counts info = DataFrameInfo( data=self, memory_usage=memory_usage, @@ -2639,7 +2647,7 @@ def info( buf=buf, max_cols=max_cols, verbose=verbose, - show_counts=null_counts, + show_counts=show_counts, ) def memory_usage(self, index=True, deep=False) -> Series: diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index 891b3ea7af0e2..ce0616f45114b 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -246,12 +246,15 @@ def to_buffer( consume the same memory amount for corresponding dtypes. With deep memory introspection, a real memory usage calculation is performed at the cost of computational resources. - null_counts : bool, optional + show_counts : bool, optional Whether to show the non-null counts. By default, this is shown only if the %(klass)s is smaller than ``pandas.options.display.max_info_rows`` and ``pandas.options.display.max_info_columns``. A value of True always shows the counts, and False never shows the counts. + null_counts : bool, optional + .. deprecated:: 1.2.0 + Use show_counts instead. Returns ------- diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index ce9aa16e57f1c..bd90d9eb0d661 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -177,9 +177,9 @@ def test_show_null_counts(self): df = DataFrame(1, columns=range(10), index=range(10)) df.iloc[1, 1] = np.nan - def check(null_counts, result): + def check(show_counts, result): buf = StringIO() - df.info(buf=buf, null_counts=null_counts) + df.info(buf=buf, show_counts=show_counts) assert ("non-null" in buf.getvalue()) is result with option_context( diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index 8c2155aec7248..7befe85850014 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -161,7 +161,7 @@ def test_info_verbose_with_counts_spacing( """Test header column, spacer, first line and last line in verbose mode.""" frame = DataFrame(np.random.randn(3, size)) buf = StringIO() - frame.info(verbose=True, null_counts=True, buf=buf) + frame.info(verbose=True, show_counts=True, buf=buf) all_lines = buf.getvalue().splitlines() # Here table would contain only header, separator and table lines # dframe repr, index summary, memory usage and dtypes are excluded @@ -480,7 +480,7 @@ def test_info_int_columns(): # GH#37245 df = DataFrame({1: [1, 2], 2: [2, 3]}, index=["A", "B"]) buf = StringIO() - df.info(null_counts=True, buf=buf) + df.info(show_counts=True, buf=buf) result = buf.getvalue() expected = textwrap.dedent( """\ From f1aafa030e86f8e390362942914152f3c38545aa Mon Sep 17 00:00:00 2001 From: Shao Yang Hong Date: Mon, 23 Nov 2020 11:12:44 +0800 Subject: [PATCH 2/5] Add test and whatsnew --- doc/source/whatsnew/v1.2.0.rst | 1 + pandas/tests/io/formats/test_format.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 727b5ec92bdc4..81bf28a979c20 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -479,6 +479,7 @@ Deprecations - The ``how`` keyword in :meth:`PeriodIndex.astype` is deprecated and will be removed in a future version, use ``index.to_timestamp(how=how)`` instead (:issue:`37982`) - Deprecated :meth:`Index.asi8` for :class:`Index` subclasses other than :class:`DatetimeIndex`, :class:`TimedeltaIndex`, and :class:`PeriodIndex` (:issue:`37877`) - The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`) +- The ``null_counts`` parameter of :meth:`DataFrame.info` is deprecated and will be removed in a future version (:issue:`37999`) .. --------------------------------------------------------------------------- diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index bd90d9eb0d661..f283407b417f6 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -194,6 +194,11 @@ def check(show_counts, result): check(True, False) check(False, False) + with tm.assert_produces_warning(FutureWarning): # GH37999 + buf = StringIO() + df.info(buf=buf, null_counts=True) + assert "non-null" in buf.getvalue() + def test_repr_truncation(self): max_len = 20 with option_context("display.max_colwidth", max_len): From d647cd74c59aa08261d0764b386e04df12c2df4e Mon Sep 17 00:00:00 2001 From: Shao Yang Hong Date: Tue, 24 Nov 2020 22:57:42 +0800 Subject: [PATCH 3/5] Fix merge --- pandas/core/frame.py | 9 ++++++--- pandas/io/formats/info.py | 10 +--------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f1c2de789cdf4..25229efd46224 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2531,14 +2531,17 @@ def to_html( is used. By default, the setting in ``pandas.options.display.max_info_columns`` is used.""" ), - null_counts_sub=dedent( + show_counts_sub=dedent( """\ - null_counts : bool, optional + show_counts : bool, optional Whether to show the non-null counts. By default, this is shown only if the DataFrame is smaller than ``pandas.options.display.max_info_rows`` and ``pandas.options.display.max_info_columns``. A value of True always - shows the counts, and False never shows the counts.""" + shows the counts, and False never shows the counts. + null_counts : bool, optional + .. deprecated:: 1.2.0 + Use show_counts instead.""" ), examples_sub=dedent( """\ diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index 9c8c09510fcd5..98bd159c567b1 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -203,15 +203,7 @@ def render( consume the same memory amount for corresponding dtypes. With deep memory introspection, a real memory usage calculation is performed at the cost of computational resources. - show_counts : bool, optional - Whether to show the non-null counts. By default, this is shown - only if the %(klass)s is smaller than - ``pandas.options.display.max_info_rows`` and - ``pandas.options.display.max_info_columns``. A value of True always - shows the counts, and False never shows the counts. - null_counts : bool, optional - .. deprecated:: 1.2.0 - Use show_counts instead. + %(show_counts_sub)s Returns ------- From 09f84384a7e9c03019ab671d8933f9d446ec30ad Mon Sep 17 00:00:00 2001 From: Shao Yang Hong Date: Wed, 25 Nov 2020 09:46:29 +0800 Subject: [PATCH 4/5] Add tests for bad combination of null_counts + show_counts --- pandas/core/frame.py | 4 +++- pandas/tests/io/formats/test_format.py | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 25229efd46224..355708eb44d40 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2645,7 +2645,9 @@ def info( show_counts: Optional[bool] = None, null_counts: Optional[bool] = None, ) -> None: - if null_counts is not None and show_counts is None: + if null_counts is not None: + if show_counts is not None: + raise ValueError("null_counts used with show_counts") warnings.warn( "null_counts is deprecated. Use show_counts instead", FutureWarning, diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index b054323e9ca11..f6fe8b85792fa 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -194,11 +194,18 @@ def check(show_counts, result): check(True, False) check(False, False) - with tm.assert_produces_warning(FutureWarning): # GH37999 + # GH37999 + with tm.assert_produces_warning( + FutureWarning, match="null_counts is deprecated.+" + ): buf = StringIO() df.info(buf=buf, null_counts=True) assert "non-null" in buf.getvalue() + # GH37999 + with pytest.raises(ValueError, match="null_counts used with show_counts"): + df.info(null_counts=True, show_counts=True) + def test_repr_truncation(self): max_len = 20 with option_context("display.max_colwidth", max_len): From aa0c57f5cb8043da19111cc05abde1cfbac67ff0 Mon Sep 17 00:00:00 2001 From: Shao Yang Hong Date: Wed, 25 Nov 2020 17:53:02 +0800 Subject: [PATCH 5/5] Clarify whatsnew --- doc/source/whatsnew/v1.2.0.rst | 2 +- pandas/core/frame.py | 2 +- pandas/tests/io/formats/test_format.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 10b99bf537f94..14ba59d19a01f 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -480,7 +480,7 @@ Deprecations - The ``how`` keyword in :meth:`PeriodIndex.astype` is deprecated and will be removed in a future version, use ``index.to_timestamp(how=how)`` instead (:issue:`37982`) - Deprecated :meth:`Index.asi8` for :class:`Index` subclasses other than :class:`DatetimeIndex`, :class:`TimedeltaIndex`, and :class:`PeriodIndex` (:issue:`37877`) - The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`) -- The ``null_counts`` parameter of :meth:`DataFrame.info` is deprecated and will be removed in a future version (:issue:`37999`) +- The ``null_counts`` parameter of :meth:`DataFrame.info` is deprecated and replaced by ``show_counts``. It will be removed in a future version (:issue:`37999`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 355708eb44d40..8b388027137d3 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2647,7 +2647,7 @@ def info( ) -> None: if null_counts is not None: if show_counts is not None: - raise ValueError("null_counts used with show_counts") + raise ValueError("null_counts used with show_counts. Use show_counts.") warnings.warn( "null_counts is deprecated. Use show_counts instead", FutureWarning, diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index f6fe8b85792fa..4f2cd6d0f80fe 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -203,7 +203,7 @@ def check(show_counts, result): assert "non-null" in buf.getvalue() # GH37999 - with pytest.raises(ValueError, match="null_counts used with show_counts"): + with pytest.raises(ValueError, match=r"null_counts used with show_counts.+"): df.info(null_counts=True, show_counts=True) def test_repr_truncation(self):