Skip to content

Commit 1746df2

Browse files
committed
Merge pull request #8701 from jreback/show_counts
ENH: provide a null_counts keyword to df.info() to force showing of null counts
2 parents 1041dff + 92c2211 commit 1746df2

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

doc/source/options.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pandas namespace. To change an option, call ``set_option('option regex', new_va
8686
pd.set_option('mode.sim_interactive', True)
8787
pd.get_option('mode.sim_interactive')
8888
89-
**Note:** that the option 'mode.sim_interactive' is mostly used for debugging purposes.
89+
**Note:** that the option 'mode.sim_interactive' is mostly used for debugging purposes.
9090

9191
All options also have a default value, and you can use ``reset_option`` to do just that:
9292

@@ -213,7 +213,8 @@ will be given.
213213
214214
``display.max_info_rows``: ``df.info()`` will usually show null-counts for each column.
215215
For large frames this can be quite slow. ``max_info_rows`` and ``max_info_cols``
216-
limit this null check only to frames with smaller dimensions then specified.
216+
limit this null check only to frames with smaller dimensions then specified. Note that you
217+
can specify the option ``df.info(null_counts=True)`` to override on showing a particular frame.
217218

218219
.. ipython:: python
219220

doc/source/whatsnew/v0.15.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Enhancements
159159
- Added support for 3-character ISO and non-standard country codes in :func:``io.wb.download()`` (:issue:`8482`)
160160
- :ref:`World Bank data requests <remote_data.wb>` now will warn/raise based on an ``errors`` argument, as well as a list of hard-coded country codes and the World Bank's JSON response. In prior versions, the error messages didn't look at the World Bank's JSON response. Problem-inducing input were simply dropped prior to the request. The issue was that many good countries were cropped in the hard-coded approach. All countries will work now, but some bad countries will raise exceptions because some edge cases break the entire response. (:issue:`8482`)
161161
- Added option to ``Series.str.split()`` to return a ``DataFrame`` rather than a ``Series`` (:issue:`8428`)
162+
- Added option to ``df.info(null_counts=None|True|False)`` to override the default display options and force showing of the null-counts (:issue:`8701`)
162163

163164
.. _whatsnew_0151.performance:
164165

pandas/core/frame.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
14241424
if buf is None:
14251425
return formatter.buf.getvalue()
14261426

1427-
def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None):
1427+
def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None):
14281428
"""
14291429
Concise summary of a DataFrame.
14301430
@@ -1444,6 +1444,12 @@ def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None):
14441444
the `display.memory_usage` setting. True or False overrides
14451445
the `display.memory_usage` setting. Memory usage is shown in
14461446
human-readable units (base-2 representation).
1447+
null_counts : boolean, default None
1448+
Whether to show the non-null counts
1449+
If None, then only show if the frame is smaller than max_info_rows and max_info_columns.
1450+
If True, always show counts.
1451+
If False, never show counts.
1452+
14471453
"""
14481454
from pandas.core.format import _put_lines
14491455

@@ -1469,8 +1475,11 @@ def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None):
14691475

14701476
max_rows = get_option('display.max_info_rows', len(self) + 1)
14711477

1472-
show_counts = ((len(self.columns) <= max_cols) and
1473-
(len(self) < max_rows))
1478+
if null_counts is None:
1479+
show_counts = ((len(self.columns) <= max_cols) and
1480+
(len(self) < max_rows))
1481+
else:
1482+
show_counts = null_counts
14741483
exceeds_info_cols = len(self.columns) > max_cols
14751484

14761485
def _verbose_repr():

pandas/tests/test_format.py

+20
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ def test_eng_float_formatter(self):
117117
repr(self.frame)
118118
self.reset_display_options()
119119

120+
def test_show_null_counts(self):
121+
122+
df = DataFrame(1,columns=range(10),index=range(10))
123+
df.iloc[1,1] = np.nan
124+
125+
def check(null_counts, result):
126+
buf = StringIO()
127+
r = df.info(buf=buf,null_counts=null_counts)
128+
self.assertTrue(('non-null' in buf.getvalue()) is result)
129+
130+
with option_context('display.max_info_rows',20,'display.max_info_columns',20):
131+
check(None, True)
132+
check(True, True)
133+
check(False, False)
134+
135+
with option_context('display.max_info_rows',5,'display.max_info_columns',5):
136+
check(None, False)
137+
check(True, False)
138+
check(False, False)
139+
120140
def test_repr_tuples(self):
121141
buf = StringIO()
122142

0 commit comments

Comments
 (0)