diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 08c528fb484c8..14ba59d19a01f 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -480,6 +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 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 6f6d94f0e9f8e..8b388027137d3 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( """\ @@ -2639,8 +2642,18 @@ 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: + if show_counts is not None: + raise ValueError("null_counts used with show_counts. Use show_counts.") + 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, @@ -2649,7 +2662,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 563dbaa06e526..98bd159c567b1 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -203,7 +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. - %(null_counts_sub)s + %(show_counts_sub)s Returns ------- diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 53d38297eafba..4f2cd6d0f80fe 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( @@ -194,6 +194,18 @@ def check(null_counts, result): check(True, False) check(False, False) + # 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=r"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): 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( """\