Skip to content

Commit

Permalink
DOC: Deprecate null_counts parameter of DataFrame.info (#37999)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongshaoyang committed Nov 25, 2020
1 parent c6eb725 commit 08913f8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,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`)

.. ---------------------------------------------------------------------------
Expand Down
21 changes: 17 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2532,14 +2532,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(
"""\
Expand Down Expand Up @@ -2640,8 +2643,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,
Expand All @@ -2650,7 +2663,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:
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
16 changes: 14 additions & 2 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/formats/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
"""\
Expand Down

0 comments on commit 08913f8

Please sign in to comment.